0

Netbeans IDE で Java アプレットを作成しました。私のアプレットはhtmlファイルを作成します。それには文字列が含まれます。

//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

一部の未読文字。

4

1 に答える 1

1

あなたのコードはFileWriter. それは常にプラットフォームのデフォルトエンコーディングを使用します - そしてそれの検出は、Netbeans で実行する場合とアプレットとして実行する場合で大きく異なる場合があります。

読み取りと書き込みの両方でエンコーディングを指定するようにコードを変更することを強くお勧めします。

  • 代わりにOutputStreamWriterラッピングを使用するFileOutputStreamFileWriter
  • Scannerコンストラクタで文字セット名を指定する
于 2013-10-18T13:47:38.673 に答える