2013-07-28 63 views
0

我有一個從HTML頁面提取西里爾文內容到文本文件。西里爾文在這個文件中是可以的。然後我使用這個文件來使用Jena創建一個RDF文件。這裏是我的代碼:使用jena庫將西里爾文寫入RDF文件

private void createRDFFile(String webContentFilePath) throws IOException { 
    // TODO Auto-generated method stub 
    Model model = ModelFactory.createDefaultModel(); 

    RDFWriter writer = model.getWriter("RDF/XML"); 
    writer.setProperty("showXmlDeclaration", "true"); 
    writer.setProperty("showDoctypeDeclaration", "true"); 
    writer.setProperty("tab", "8"); 
    Writer out = new BufferedWriter(new OutputStreamWriter(
      new FileOutputStream(rdfFilePath), "UTF8")); 
    Resource resDest = null; 
    Property hasTimeStart = model.createProperty(ns + "#hasTimeStart"); 
    Property distrName = model.createProperty(ns + "#distrName"); 
    Property moneyOneDir = model.createProperty(ns + "#moneyOneDir"); 
    Property moneyTwoDir = model.createProperty(ns + "#moneyTwoDir"); 
    Property hasTimeStop = model.createProperty(ns + "#hasTimeStop"); 

    BufferedReader br = new BufferedReader(new FileReader(
      webContentFilePath)); 
    String line = ""; 
    while ((line = br.readLine()) != null) { 
     String[] arrayLine = line.split("\\|"); 
     resDest = model.createResource(ns + arrayLine[5]); 
     resDest.addProperty(hasTimeStart, arrayLine[0]); 
     resDest.addProperty(distrName, arrayLine[1]); 
     resDest.addProperty(moneyOneDir, arrayLine[2]); 
     resDest.addProperty(moneyTwoDir, arrayLine[3]); 
     resDest.addProperty(hasTimeStop, arrayLine[4]); 
    } 
    br.close(); 
    model.write(System.out, "RDF/XML"); 
    writer.write(model, out, null); 

} 

當我打開RDF文件西里爾就像РўРРђРќРЎРљРћРџ-Р'РРўРћР>Рђ? 有人可以幫我嗎?

回答

2

輸出寫入器上的UTF-8寫入編碼看起來是正確的,所以這表明您沒有閱讀webContentFilePath正確的編碼。作爲診斷,您可以嘗試只讀取該文件,然後將其寫入純文本UTF-8文件(無RDF)。我的猜測是,您必須明確如何設置br上的文件編碼,或確保已開始使用UTF-8書寫已刪除的網頁。

+0

我錯了。首先我忘記了OutputStreamWriter中的utf-8編碼設置,然後我沒有在我打開的文本編輯器中重新加載文件。現在在文本編輯器中是OK的,但在Eclipse中我仍然收到這些奇怪的字符。 – vikifor

+2

所以你的文件是可以的,除非你在Eclipse中打開它?您應該將Eclipse的默認編碼設置爲UTF-8,請參閱http://www.eclipse.org/forums/index.php/t/29511/以獲得一些建議或其他類似主題的StackOverflow問題。 –

1

這可能是輸出是正確的,但你沒有看到它正確。

新的FileReader(...)將以平臺默認字符集打開文件。這不是Windows上的UTF-8,所以如果它看起來不錯,那麼你可以用UTF-8以外的其他方式查看它。

Jena默認在UTF-8中寫入,在這種情況下。

因此,當您編寫文件時,無法像查看輸入一樣查看它。您需要使用支持UTF-8的查看器查看它。