Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filewriter not writing Json String into new File

I am trying to write a Json String into a File, using FileWriter.
Apparently nothing happens when executing the code.
The Path to my directory is correct.
I also tried different options like Files.writeString(), but it didnt work.
I recreated an example, where I have the same problem like in my actual code:

            FileWriter fw = new FileWriter("C:\\User\\lulac\\IdeaProjects\\OOS_Praktikum\\JsonFiles\\test.json");
            String content ="[\n" +
                    "  {\n" +
                    "    \"CLASSNAME\": \"OUTGOINGTRANSFER\",\n" +
                    "    \"INSTANCE\": {\n" +
                    "      \"sender\": \"Nico\",\n" +
                    "      \"recipient\": \"Angelo\",\n" +
                    "      \"date\": \"12.01.2020\",\n" +
                    "      \"amount\": 150.0,\n" +
                    "      \"description\": \"Strafzahlung\"\n" +
                    "    }\n" +
                    "  }\n" +
                    "]\n";
            fw.write(content);
            fw.close();

Happy about your help

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It is a typical German error: it must be Users not User (in German the plural of words ending in "er" remain the same). However: FileWriter is the incorrect class; it will write in the local Windows encoding, and JSON uses UTF-8.

As this is home work: use Files.writeString which uses UTF-8 by default.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading