簡體   English   中英

使用java和RDF模型從文件讀取和寫入

[英]read and write from file using java and the RDF model

我想從一個文件中讀取然后使用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");

任何人都可以幫忙

寫入FileOutputStream而不是System.out。 您可能希望“RDF-XML-ABBREV”用於美化輸出。

作為一般的改進,使用RDFDataMgrLang.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; 它更容易閱讀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM