read and write from file using java and the RDF model

1.1k Views Asked by At

I want to read from a file then write it in a RDF/XML model in another file using java, I've done the initial part in reading the file, but i didn't know how to write it in another file withe the RDF/XML model so I cam have the correct format.

here is part of the read file code:

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");

can anyone help please

1

There are 1 best solutions below

4
On BEST ANSWER

Write to a FileOutputStream not System.out. You may want "RDF-XML-ABBREV" for prettified output.

As a general improvement, use RDFDataMgr with Lang.RDFXML (which is by default the pretty form):

For Java these days that means use a try-resource block:

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();
    }

Final consideration: use Turtle, not RDF/XML; it's easier to read.