我想问一下,在Java中是否有一种方法可以读取,基本上是任何文件格式(N3、JSON、RDF)等等,然后将其转换为then (.ttl)。我在Google上搜索了一些想法,但他们主要解释的是特定的文件类型以及如何将文件类型转换为RDF,而我想要的是另一种方式。
编辑(按照答案中给出的代码示例):
if(FilePath.getText().equals("")){
FilePath.setText("Cannot be empty");
}else{
try {
// get the inputFile from file chooser and setting a text field with
// the path (FilePath is the variable name fo the textField in which the
// path to the selected file from file chooser is done earlier)
FileInputStream fis = new FileInputStream(FilePath.getText());
// guess the format of the input file (default set to RDF/XML)
// when clicking on the error I get take to this line.
RDFFormat inputFormat = Rio.getParserFormatForFileName(fis.toString()).orElse(RDFFormat.RDFXML);
//create a parser for the input file and a writer for Turtle
RDFParser rdfParser = Rio.createParser(inputFormat);
RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE,
new FileOutputStream("./" + fileName + ".ttl"));
//link parser to the writer
rdfParser.setRDFHandler(rdfWriter);
//start the conversion
InputStream inputStream = fis;
rdfParser.parse(inputStream, fis.toString());
//exception handling
} catch (FileNotFoundException ex) {
Logger.getLogger(FileConverter.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileConverter.class.getName()).log(Level.SEVERE, null, ex);
}
}我已经将"eclipse-rdf4j-3.0.3-onejar.jar“添加到NetBeans中的Libraries文件夹中,现在当我运行该程序时,我一直收到以下错误:
org.eclipse.rdf4j.common.lang.service.ServiceRegistry.(ServiceRegistry.java:31)线程“AWT 0”中的异常java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
如有任何帮助或建议,将不胜感激。谢谢。
发布于 2019-12-29 06:05:16
是的,这是可能的。一种选择是为此目的使用月食RDF4J,或者更具体地说,使用它的里约解析器/作家工具包。
下面是一个使用RDF4J Rio的代码示例。它根据文件扩展名检测输入文件的语法格式,并以Turtle语法直接将数据写入新文件:
// the input file
java.net.URL url = new URL(“http://example.org/example.rdf”);
// guess the format of the input file (default to RDF/XML)
RDFFormat inputFormat = Rio.getParserFormatForFileName(url.toString()).orElse(RDFFormat.RDFXML);
// create a parser for the input file and a writer for Turtle format
RDFParser rdfParser = Rio.createParser(inputFormat);
RDFWriter rdfWriter = Rio.createWriter(RDFFormat.TURTLE,
new FileOutputStream("/path/to/example-output.ttl"));
// link the parser to the writer
rdfParser.setRDFHandler(rdfWriter);
// start the conversion
try(InputStream inputStream = url.openStream()) {
rdfParser.parse(inputStream, url.toString());
}
catch (IOException | RDFParseException | RDFHandlerException e) { ... }有关更多示例,请参见RDF4J文档。
编辑有关您的NoClassDefFoundError:您在类路径上缺少了一个必要的第三方库(在本例中,是日志库)。
与其使用onejar,不如使用Maven (或Gradle)来设置项目。请参阅开发环境设置注释,或者更多的一步一步的指南,请参阅本教程 (本教程使用的是Eclipse而不是Netbeans,但关于如何设置maven项目的要点在Netbeans中非常相似)。
如果您真的不想使用Maven,也可以只使用下载RDF4J SDK,这是一个ZIP文件。打开它,只需将lib/目录中的所有jar文件添加到Netbeans。
发布于 2020-02-22 09:19:02
另一种选择是使用阿帕奇·耶娜。
https://stackoverflow.com/questions/59514022
复制相似问题