2013-05-27 67 views
1

我遇到與我的jena庫的寫入方法有關的問題。 我有下面這段代碼應該寫在外部文件的輸出,但它不這樣做。Jena庫不會將輸出寫入外部RDF/XML文件

import com.hp.hpl.jena.rdf.model.*; 
import com.hp.hpl.jena.vocabulary.*; 
import com.hp.hpl.jena.rdf.model.impl.ModelCom; 


public class Tutorial04 extends Object { 

// some definitions 
static String tutorialURI = "http://hostname/rdf/tutorial/"; 
static String briansName = "Brian McBride"; 
static String briansEmail1 = "[email protected]"; 
static String briansEmail2 = "[email protected]"; 
static String title  = "An Introduction to RDF and the Jena API"; 
static String date   = "23/01/2001"; 

@SuppressWarnings("unused") 
public static void main (String args[]) { 

    // some definitions 
    String personURI = "http://somewhere/JohnSmith"; 
    String givenName = "John"; 
    String familyName = "Smith"; 
    String fullName  = givenName + " " + familyName; 
    // create an empty model 
    Model model = ModelFactory.createDefaultModel(); 

    // create the resource 
    // and add the properties cascading style 
    Resource johnSmith 
     = model.createResource(personURI) 
      .addProperty(VCARD.FN, fullName) 
      .addProperty(VCARD.N, 
          model.createResource() 
           .addProperty(VCARD.Given, givenName) 
           .addProperty(VCARD.Family, familyName)); 

    // now write the model in XML form to a file 
    // model.write(System.out, "RDF/XML"); 
    model.write(System.out,"RDF/XML"); 
} 

}

回答

9

您有:

model.write(System.out,"RDF/XML"); 

它說「請寫這個模型的內容到標準輸出」即不是任何命名的文件。要將模型寫入文件,您需要說出以下文件:

String fileName = "your_file_name_here.rdf"; 
FileWriter out = new FileWriter(fileName); 
try { 
    model.write(out, "RDF/XML-ABBREV"); 
} 
finally { 
    try { 
     out.close(); 
    } 
    catch (IOException closeException) { 
     // ignore 
    } 
} 
+0

謝謝Ian :)它有幫助。 另外,我會在我的最後一年的項目中需要你一些更多的幫助。你還有耶拿圖書館的專門知識嗎? –

+0

很高興有幫助。如果您需要Jena的其他幫助,您可以繼續在此處提問或向Jenu用戶組發送消息:請參閱http://jena.apache.org/help_and_support/index.html –

+0

Hi Ian。我可以有你的電子郵件地址嗎? 本週,我將需要幫助在jena的一些任務 –