0

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?

Aristos
  • 66,005
  • 16
  • 114
  • 150
G3oRge
  • 19
  • 2

5 Answers5

1

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) {
}
Sig
  • 4,988
  • 3
  • 28
  • 29
1

try it with that:

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(yourFilePath,"UTF8"));
out.write(text); 
out.close();
Neifen
  • 2,546
  • 3
  • 19
  • 31
1

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.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
1

After a lot of searching, I solved that problem.

In .bat file just put:

Set JAVA_TOOL_OPTIONS= -Dfile.encoding=UTF8
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
G3oRge
  • 19
  • 2
0

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?

Community
  • 1
  • 1