read and write from file using java and the RDF model

Clash Royale CLAN TAG#URR8PPPread and write from file using java and the RDF model
I want to read from a file then write it in a RDF/XML model in a file using java,
I've done the initial part in reading the file, but i didn't know how to write it in a 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 Answer
1
Write to a FileOutputStream not System.out. You may want "RDF-XML-ABBREV" for prettified output.
FileOutputStream
As a general improvement, use RDFDataMgr with Lang.RDFXML (which is by default the pretty form):
RDFDataMgr
Lang.RDFXML
For Java these days that means use a try-resource block:
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();
}
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.
Yes - example corrected and updated.
– AndyS
15 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
should I use try and catch for it also?
– Ghas
1 hour ago