Moscoot
Moscoot

Reputation: 73

Can I write Strings into a .JSON-File without using JSON Library

I need to write formatted Strings into a .json File without using the JSON library. If I try to use the following code to write a test string I don't get a result inside the .json-File.

FileWriter file = new FileWriter("C:\\Test.json");` 
file.write("test");

Upvotes: 0

Views: 452

Answers (1)

Nico S.
Nico S.

Reputation: 151

  1. Json looks something like that:
{
    "glossary": {
        "title": "example glossary",
      "GlossDiv": {
            "title": "S",
          "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                  "SortAs": "SGML",
                  "GlossTerm": "Standard Generalized Markup Language",
                  "Acronym": "SGML",
                  "Abbrev": "ISO 8879:1986",
                  "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                      "GlossSeeAlso": ["GML", "XML"]
                    },
                  "GlossSee": "markup"
                }
            }
        }
    }
}

You have key-value pairs there. Then you use Brackets to define the structure.

  1. You need to use something like file.flush() when you want to write.

Explanation: When you are writing to a file, Java does not want to send each letter to it individually. Therefore it stores them until an certain point is reached if you dont reach that point it wont send the data to the file. To force it, to send the Data use the flush()-Method.

Upvotes: 1

Related Questions