简体   繁体   中英

Save to file with special characters

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM