英文:
Write in Real Time and Remove last character in file JAVA
问题
我正在将一个txt文件转换为json格式,我正在使用json-simple库。
我希望文件能够实时写入,也就是说,每一行都写入一次。为此,我选择不使用JsonArray,
因为如果我使用JsonArray,我必须等它完全生成后才能将其写入文件。
所以我只使用JsonObjects。
问题是我必须创建一个“隐藏”的JsonArray,在文件开头和结尾添加方括号,然后对每个JsonObject添加一个逗号。
问题是显然在文件末尾在“]”之前也打印出了逗号,我该如何去掉最后的逗号?
br = new BufferedReader(new FileReader(pathFile + ".txt"));
JSONObject stringDetails = new JSONObject();
JSONArray stringList = new JSONArray();
try (FileWriter file = new FileWriter(pathfile + ".json", true)) {
file.write("[");
while ((line = br.readLine()) != null) {
// 缺少从txt字符串转换为json字符串的代码...
stringDetails.put(stringJson.getKey(), stringJson.getMessage());
file.write(String.valueOf(stringDetails) + ",");
stringDetails = new JSONObject();
}
file.write("]");
}
另一个问题是,在使用append (true)时,如果程序以异常方式停止,之前的所有字符串都会被保存吗?
非常感谢。
英文:
i'm converting a txt in json, and i'm using json-simple.
I want the file to be written in real time, that is, each line, for this i chose to dont use JsonArray,
because if I use the JSONArray, I have to wait for it to be complete first and then write it to the file.
So I'm only using JsonObjects.
The problem that I have to create a "hidden" JsonArray, for this at the beginning and end of file I add square brackets , and then to each JsonObject I add a comma.
The problem that obviously the comma is printed even at the end of the file before "]", how do I remove the last comma?
br = new BufferedReader(new FileReader(pathFile + ".txt"));
JSONObject stringDetails = new JSONObject();
JSONArray stringList = new JSONArray();
try (FileWriter file = new FileWriter(pathfile+".json",true)) {
file.write("[");
while ((line = br.readLine()) != null) {
//Miss the code to convert from txt string to json string ...
stringDetails.put(stringJson.getKey(), stringJson.getMessage());
file.write(String.valueOf(stringDetails)+",");
stringDetails = new JSONObject();
}
file.write("]");
}
A other question is, using the append (true), in case if the program stopped in anomaly way, all the strings before are saved?
Thanks so much.
答案1
得分: 0
第一种方法:使用一个布尔变量和一个while循环中的if语句,在条目之前(除了第一个条目)打印逗号
boolean isFirst = true;
file.write("[");
while ((line = br.readLine()) != null) {
//缺少将文本字符串转换为JSON字符串的代码...
stringDetails.put(stringJson.getKey(), stringJson.getMessage());
if (isFirst) {
isFirst = false;
} else {
// 在新条目之前打印逗号
file.write(",");
}
file.write(String.valueOf(stringDetails));
stringDetails = new JSONObject();
}
file.write("]");
第二种方法:另一种方法是创建一个私有的辅助方法,将条目打印到文件中,如下:
private static void printEntry(FileWriter file, String line, ... /*还需要其他参数*/) {
//缺少将文本字符串转换为JSON字符串的代码...
stringDetails.put(stringJson.getKey(), stringJson.getMessage());
file.write(String.valueOf(stringDetails));
stringDetails = new JSONObject();
}
然后在循环之外使用这个方法,将第一个条目的写入从while循环中提取出来,如下:
file.write("[");
if ((line = br.readLine()) != null) {
// 打印第一个条目
printEntry(file, line, ...);
// 打印剩余的条目
while ((line = br.readLine()) != null) {
file.write(",");
printEntry(file, line, ...);
}
}
file.write("]");
英文:
I see two possible ways and both are essential the solution to not print it in the first place.
First: use a boolean
and an if-statement in the while loop to print the commas before the entries (except for the first one)
boolean isFirst = true;
file.write("[");
while ((line = br.readLine()) != null) {
//Miss the code to convert from txt string to json string ...
stringDetails.put(stringJson.getKey(), stringJson.getMessage());
if (isFirst) {
isFirst = false;
} else {
// print the comma before the new entry
file.write(",");
}
file.write(String.valueOf(stringDetails));
stringDetails = new JSONObject();
}
file.write("]");
Second: A second approch would be to have a private helper method to print the entry to the file, like:
private static void printEntry(FileWriter file, String line, ... /* what ever else you need*/) {
//Miss the code to convert from txt string to json string ...
stringDetails.put(stringJson.getKey(), stringJson.getMessage());
file.write(String.valueOf(stringDetails));
stringDetails = new JSONObject();
}
and use this in to extract the writing for the first entry from the while loop, like:
file.write("[");
if ((line = br.readLine()) != null) {
// print first entry
printEntry(file, line, ...);
// print the rest
while ((line = br.readLine()) != null) {
file.write(",");
printEntry(file, line, ...);
}
}
file.write("]");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论