Sandy
Sandy

Reputation: 163

How to create new text file on the fly if one file overflow in java

I am writing json data into one file. It works fine for small data but for large data it is writing some amount of data and remaining data gets skipped. So Please guide me how to create new file if one gets fulled in java? Currently i am using below code to write json object into a file.

Below is the sample which is quite similar to my working code.

 String json=null;
                for(int counter=0;counter<2;counter++){
                JSONObject obj=new JSONObject();
                JSONObject second=new JSONObject();
                obj.put("ClassName", "sample.com");
                obj.put("Query", "query");
                obj.put("Message", "Successfylly");

                second.put("Number"+sequence++, obj);
                company.add(second);
                //file.write(obj.toJSONString());
                Gson gson = new GsonBuilder().setPrettyPrinting().create();
                json = gson.toJson(company);
                 file.write(json);
                file.close();

                }

Please guide me how to create new file if existing file gets fulled while writing json object into a text file.

Upvotes: 0

Views: 278

Answers (1)

Simon DeWolf
Simon DeWolf

Reputation: 388

It's difficult to tell, as your code is not formatted well, but it looks like every iteration of your loop is overwriting the variable json. Only the value of json at the last iteration of the loop will be written to the file.

Move the call to file.write(json) inside the loop.

Upvotes: 1

Related Questions