1

我想从一个文件中读取,然后使用 java 将其写入另一个文件中的 RDF/XML 模型,我已经完成了读取文件的初始部分,但我不知道如何使用 RDF 将它写入另一个文件/XML 模型,因此我可以使用正确的格式。

这是读取文件代码的一部分:

try {
        File file1 = new File("Data/960.txt");
    FileReader fileReader1 = new FileReader(file1);
    BufferedReader bufferedReader1 = new BufferedReader(fileReader1);
    StringBuffer stringBuffer = new StringBuffer();
    String line1;
        System.out.println("Proteins & Synonyms:");
        int count = 0;
            while ((line1=bufferedReader1.readLine()) != null) {
                            String[] list1 = line1.split("\t");
                            if (list1.length < 2) continue;

                        proteinG=model2.createResource(ProtURI+list1[0]);
                        hasSynonyms=model2.createProperty(SynoURI+hasSynonymStr);
                        Synonyms=list1[1];
                        proteinG.addProperty(hasSynonyms,Synonyms); 

            System.out.println(stringBuffer.toString());

        } catch (IOException e) {
            e.printStackTrace();}

    if(model2!=null){
            model2.write(System.out, "RDF/XML");

谁能帮忙

4

1 回答 1

3

写入FileOutputStream非 System.out。您可能需要“RDF-XML-ABBREV”来美化输出。

作为一般改进,使用RDFDataMgrwith Lang.RDFXML(默认情况下是漂亮的形式):

对于如今的 Java,这意味着使用 try-resource 块:

import org.apache.jena.riot.RDFDataMgr
import org.apache.jena.riot.Lang  
...

    Model model = null;
    try(OutputStream out = new FileOutputStream("filename.rdf")) {
        RDFDataMgr.write(out, model, Lang.RDFXML);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

最后考虑:使用 Turtle,而不是 RDF/XML;它更容易阅读。

于 2018-07-29T15:50:03.670 回答