0

I've a FileOutputStream and a cicle like that:

PrintStream output = new PrintStream(new FileOutputStream("XXXX.json"));

output.print("{\"ObjectsList\": [");

for (int i = 0; i < something.length; i++) { 
     output.print("{"AAAA"},");
}

output.print("]}")

But I don't need the char "," before that output.print("]}");

How can I write at EOF-1 or EOF-2 for overwrite the last char ","

3 Answers 3

2

Stop the loop at something.length - 1 and make a last call to print with output.print("{ AAAA"}");

Sign up to request clarification or add additional context in comments.

1 Comment

"AAAA" its only an example last value of something must be scansioned and added cant stop at something.length - 1
0
   String var = "";
   var = "{\"ObjectsList\": [";

    for (int i = 0; i < something.length; i++) { 
         var = "{"AAAA"},";
    }

    //var.substring(0, var.length()-1) => remove the last char
    output.print(var.substring(0, var.length()-1) + "]}");

1 Comment

hmm! In your "for", you can put a if to print with no "," when the element is the last. But i think the Fabio's answer is better.
0

Try something like this:

 output.println("{\" ObjectsList \": [");
    for(int i=0;i<something.length;i++){
        String out="{\""+i+"}";

        if(i!=something.length-1 ){
            out+=", ";
        }
        output.println(out);
    }
    output.print("]}");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.