in an application i write some strings into a file. Some of them are in greek language. When i make this process with Netbeans, all work great. But when i try to run it with my .bat file, the same code writes the greek text with that strange "�?" character. Obviously netbeans regulate something and it can writes the Greek text, but i can't find it.
Example开发者_开发百科 of write:
BufferedWriter out = new BufferedWriter(new FileWriter(FilePath));
out.write(text);
out.close();
The compile.bat and run.bat files are the simplest form they can get.
What can i do to fix this?
You need to output in UTF-8 try:
try {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(FilePath), "UTF8"));
out.write(text);
out.close();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
try it with that:
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(yourFilePath,"UTF8"));
out.write(text);
out.close();
When you say you see the Greek characters fine with NetBeans, you are probably writing output to the NetBeans console, which is a GUI window capturing standard output. Windows generally has no problems rendering characters in these environments as the fonts are quite rich.
When you say you used a .BAT file, you were probably writing to the Windows console (cmd.exe
or Powershell). You can search for help on getting the Windows console to writing non-ASCII text; several sources exist that explain "code pages" and other things to know. Basically it comes down to ensuring your console is set up properly.
After a lot of searching, I solved that problem.
In .bat file just put:
Set JAVA_TOOL_OPTIONS= -Dfile.encoding=UTF8
I had the same problem and this code solved it :
System.setProperty("file.encoding","UTF-8");
Field charset = Charset.class.getDeclaredField("defaultCharset");
charset.setAccessible(true);
charset.set(null,null);
Setting the default Java character encoding?
精彩评论