NickName
NickName

Reputation: 313

Jar change outputs unicode

I have create a java applet in Netbeans IDE. My applet create a html file. That include a string.

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

My template.html is :

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

When i run it from Netbeans and create the example.html file when i open it i see

Δεῦτε (it's ancients greek characters) in my broswer but when i create the applet.jar and run the .jar it create the same example.html file but when i open it in my broswer i see something else like :

xC4xE5?xF4xE5

some unread characters.

Upvotes: 0

Views: 196

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500495

Your code uses FileWriter. That will always use the platform default encoding - and the detection of that may well vary between running it in Netbeans and running it as an applet.

I would strongly suggest that you change the code to specify the encoding when you both read and write:

  • Use OutputStreamWriter wrapping FileOutputStream instead of FileWriter
  • Specify the charset name in the Scanner constructor

Upvotes: 1

Related Questions