Reputation: 2245
I have problem with encoding in Java. I have set encoding in eclipse UTF-8. When I run my app from eclipse everything is ok but when I exported to jar and run it by double clicking I have ???? characters. When I run from command line: java -jar app.jar everything is ok. The problem is with downloaded data from other site (the site is utf8 encoded). What's the solution ?
EDIT: On all platforms, when I run from command line the defaultEncoding() is UTF-8. But when I run by double clicking: Mac: US-ASCII Windows: windows-1250
I have wrote encoding method but it still not working:
public String getPageContent(String url) throws MalformedURLException, IOException
{
URL urlReader;
InputStream response = null;
BufferedReader reader;
String pageContent = "";
urlReader = new URL(url);
response = urlReader.openStream();
reader = new BufferedReader(new InputStreamReader(response));
for (String line; (line = reader.readLine()) != null;) {
pageContent += this.encode(line, "UTF-8");
}
reader.close();
return pageContent;
}
public String encode(String s, String charset)
{
try {
byte[] b = s.getBytes(charset);
s = new String(b, charset);
return s;
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return s;
}
Upvotes: 1
Views: 3624
Reputation: 20631
You need to specify the UTF-8 character set when you construct the InputStreamReader.
reader = new BufferedReader(new InputStreamReader(response, "UTF-8"));
You shouldn't be trying to re-encode strings after having received them at all.
Upvotes: 3
Reputation: 1093
Setting the default Java character encoding? Here is already a discussed thread with more details. Hope this help
Upvotes: 0