0

My program works fine in my MAC, but when I try it on WINDOWS all the special characters turns into %$&.. I am norwegian so the special characters is mostly æøå.

This is the code I use to write to file:

    File file = new File("Notes.txt");
    if (file.exists() && !file.isDirectory()) {
        try(PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("Notes.txt", true)))) {

            pw.println("");
            pw.println("*****");
            pw.println(notat.getId());
            pw.println(notat.getTitle());
            pw.println(notat.getNote());
            pw.println(notat.getDate());

            pw.close();

        }catch (Exception e) {
            //Did not find file
        }
    } else {
        //Did not find file
    }

Now how can I assure that the special characters gets written correct in both OS?

NOTE: I use IntelliJ, and my program is a .jar file.

Peter
  • 1,848
  • 4
  • 26
  • 44
  • Hei! What do you use to view your file after it has been written (on Windows)? It sounds like a disagreement on character set. If so, either your Java program uses the wrong character set for Winodws, or your viewer does. I see nothing wrong with your Java code in this respect. – Ole V.V. Mar 23 '16 at 10:17
  • Just any text editor, open office, notepad.. anything – Peter Mar 23 '16 at 10:19
  • Strange … Is there a Windows expert in the room? :-) – Ole V.V. Mar 23 '16 at 10:25
  • @OleV.V. Can this problem occur when saved to a txt file? Do you think it can it be solved by saving to e rich txt file, rtf? – Peter Mar 23 '16 at 10:38
  • Your text editor is bad at guessing the right charset. Start your java programm with `-Dfile.encoding=CP1252`. This is the default windows chartset (in europe). – Peter Mar 23 '16 at 10:41
  • @Peter Where should I put this line? In the Main method? iml or xml file? – Peter Mar 23 '16 at 10:51
  • I guess your are starting the app using `IDEA`? If so, put that line into the `VM options:` of the appropriate run/debug configuration. – Peter Mar 23 '16 at 10:56

1 Answers1

2

Make sure that you use the same encoding on windows as you do on mac.

IDEA displays the encoding in the right lower corner. Furthermore, you can configure the encoding Settings -> Editor -> File Encodings. It's possible to configure the encoding project wide or per file.

Furthermore, read java default file encoding to make sure, reading and writing files will always use the same charset.

Community
  • 1
  • 1
Peter
  • 4,752
  • 2
  • 20
  • 32