2013-10-18 66 views
0

我已經在Netbeans IDE中創建了一個java applet。我的小程序創建一個html文件。包括一個字符串。Jar更改輸出unicode

//Other code 
File htmlTemplateFile = new File("template.html"); 
    String htmlString = FileUtils.readFileToString(htmlTemplateFile); 
    String title = "Title"; 

    htmlString = htmlString.replace("$title", title); 

    File newTextFile = new File("paragraph.txt"); 
    FileUtils.write(newTextFile, "Contents", "UTF-8"); 
    FileWriter pw = new FileWriter(newTextFile); 

    pw.write("Δεῦτε"); 
    pw.close(); 
    String paragraph = new Scanner(new File("paragraph.txt")).useDelimiter("\\A").next(); 


    htmlString = htmlString.replace("$paragraph", paragraph); 

    File newHtmlFile = new File("example.html"); 
    FileUtils.writeStringToFile(newHtmlFile, htmlString); 

我template.html是:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>$title</title> 
</head> 
<body> 
<p>$paragraph 
</p> 
</body> 
</html> 

當我從NetBeans中運行它,並創建example.html的文件,當我打開它,我看到

Δεῦτε(它的古人希臘字符)在我的大衣,但是當我創建applet.jar並運行.jar它創建相同的example.html文件,但是當我打開它在我的大衣,我看到其他類似的:

xC4xE5?xF4xE5

一些未讀字符。

回答

1

您的編碼使用FileWriter。這將始終使用平臺默認編碼 - 並且在Netbeans中運行它並將它作爲小程序運行之間的檢測可能會有所不同。

我強烈建議你更改代碼到指定編碼,當你讀取和寫入:

  • 使用OutputStreamWriter包裝FileOutputStream代替FileWriter
  • Scanner構造函數中指定字符集名稱
+0

我改變它,但不幸的是我仍然有同樣的問題。 – NickName

+0

此時您應該做更多的診斷 - 嘗試用Unicode值逐個打印字符(例如將它們轉換爲int)。這樣你就可以找到事情出錯的地方。 –