0

我有一个FileOutputStream像这样的cicle:

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

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

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

output.print("]}")

但我不需要char ","在那之前output.print("]}");

如何在 EOF-1 或 EOF-2 写入以覆盖最后一个字符","

4

3 回答 3

2

停止循环something.length - 1并进行最后一次打印调用output.print("{ AAAA"}");

于 2013-11-12T20:42:45.970 回答
0

尝试这样的事情:

 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("]}");
于 2013-11-12T22:03:11.703 回答
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) + "]}");
于 2013-11-12T20:48:23.327 回答