Yes because the model.write() works but the FileWriter does not. I also tried something like this to append data but failed:
FileOutputStream f1=new FileOutputStream(file, *true*); Any solution please you suggest to write it to the same file I have read from? On Fri, Jul 22, 2016 at 2:22 AM, Lorenz B. < [email protected]> wrote: > Hello Dave, > > I think the problem is that he writes to another file - and I guess this > one already contains some data. Of course the write method cannot append > data, but just overwrites the file. Am I right? Anything else would > sound like some magic. > > Otherwise, to append data, one would have to load it into a separate > model and add data directly to this model. > > > > Kind regards, > Lorenz > > > On 22/07/16 09:58, kumar rohit wrote: > >> The main() pass the required file and then just the GUI loads because we > >> have to deal with a text field where user enter his name and we will > >> create > >> individual of that name. > >> > >> The main purpose of this code is to create individual (after user > >> enter the > >> name) and store it locally in a file *(either a separate file or to the > >> file we have read **InputStream in =FileManager.get().open(file);*) > >> > >> It writes the required result to the console(model.write method) but > >> when I > >> try to write it in a file, it just remove all the data from the > >> destination > >> file and even the ontology URI. > >> > >> For writing to a file, I used these both methods but both does not work. > >> > >> (1) FileWriter out = new FileWriter( fileName ); > >> > >> try { > >> model.write( out, "RDF/XML-ABBREV" );} > >> > >> 2) FileOutputStream out=new FileOutputStream(filename); > >> RDFWriter d = model.getWriter("RDF/XML-ABBREV"); > >> d.write(model,out,null); > >> }catch(Exception e) {} > > > > > > Make sure you close the stream, that's probably the source of your > > random loss of output. > > > > The reason you see Seq and other such junk is because you are writing > > the whole model, with inference, and a default OntModel includes RDFS > > inference. Use model.write which for an OntModel defaults to writing > > just the base model. Also, unless you need inference use an explicit > > OntModelSpec which doesn't have any (e.g. OWL_MEM). > > > > Dave > > > >> On Fri, Jul 22, 2016 at 12:30 AM, Lorenz B. < > >> [email protected]> wrote: > >> > >>> 1.) This is not a minimal running example! that means, nobody can test > >>> it without some (in your case too much) effort to get it running. > >>> 2.) w.r.t. 1), look at your main() method, it reads a file and then? > >>> 2.) Please tell us exactly WHAT does not work and WHAT you expect. > >>> 3.) The OntModel object has a method write, not sure what you did there > >>> with calling getWriter and the like > >>> > >>>> *When I run this program without writing it to the file, it runs > >>> smoothly. > >>>> But when I copy it to a file (both newly created and existing file), > >>>> sometime it adds the garbage claases to the file like Seq, Bag, > >>>> Resource > >>>> and most of the time it does nothing except it empties the destination > >>>> file.* > >>>> > >>>> *I have copied the whole code except the code auto generated by > >>>> netbeans* > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> *public class NewJFrame extends javax.swing.JFrame {* > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> * public static void readOntology(String file,OntModel model)* > >>>> > >>>> * {* > >>>> > >>>> > >>>> * InputStream in =FileManager.get().open(file);* > >>>> > >>>> > >>>> > >>>> *if (in==null) {* > >>>> > >>>> *throw new IllegalArgumentException( "File: " + file + " not > >>>> found");* > >>>> > >>>> *}* > >>>> > >>>> * model.read(in,null);* > >>>> > >>>> * //in.close();}}* > >>>> > >>>> > >>>> > >>>> private void read1(String str, OntModel model) > >>>> > >>>> { > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> String ns=" > >>>> http://www.semanticweb.org/ontologies/untitled-ontology-54/"; > >>>> > >>>> > >>>> OntClass user = model.createClass(ns + "User"); > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> OntClass user1 = model.getOntClass(ns + "User"); > >>>> > >>>> Individual janeSmith = user1.createIndividual(ns + str) > >>>> > >>>> > >>>> > >>>> try { > >>>> > >>>> String file="F://newontology.owl"; > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> FileOutputStream f1=new FileOutputStream(file); > >>>> > >>>> RDFWriter d = model.getWriter("RDF/XML-ABBREV"); > >>>> > >>>> d.write(model,f1,null); > >>>> > >>>> }catch(Exception e) {} > >>>> > >>>> > >>>> *private void jButton1ActionPerformed(java.awt.event.ActionEvent > >>>> evt) { > >>>> * > >>>> > >>>> > >>>> > >>>> *OntModel model=ModelFactory.createOntologyModel();* > >>>> > >>>> > >>>> * String str=jTextField2.getText()* > >>>> > >>>> > >>>> > >>>> * read1(str, model); }}* > >>>> > >>>> > >>>> > >>>> * public static void main(String args[]) {* > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> > >>>> * OntModel model = ModelFactory.createOntologyModel();* > >>>> > >>>> * String file="F://updated.owl";* > >>>> > >>>> * readOntology(file , model );* > >>>> > >>>> *}* > >>>> > >>> -- > >>> Lorenz Bühmann > >>> AKSW group, University of Leipzig > >>> Group: http://aksw.org - semantic web research center > >>> > >>> > >> > > > -- > Lorenz Bühmann > AKSW group, University of Leipzig > Group: http://aksw.org - semantic web research center > >
