我尝试创建一个Java应用程序,将Excel / csv文件内容转换为JSON格式, 因为我的所有输出json文件都有相同的标题。我选择使用BufferedReader和BufferedWriter的经典方法进行简化。这是我的代码的一部分:
BufferedReader csvFile= new BufferedReader(new FileReader("DataTextCsv.csv"));
BufferedWriter jsonFile=new BufferedWriter(new FileWriter("converted.txt"));
String fileContent = csvFile.readLine();
// set the constant header
String jsonScript="constant header of json content";
while (fileContent !=null){
fileContent=csvFile.readLine();
String[] tab = fileContent.split(",");
// variable content from csv file
jsonScript+="\""+tab[0]+"\" :";
jsonScript+=tab[1]+","+"\n";
// End of json content construction
jsonScript=jsonScript.substring(0,jsonScript.length()-2);
jsonScript+="}";
String[] tabWrite=jsonScript.split("\n");
for (String item:tabWrite){
jsonFile.write(item);
jsonFile.newLine();
}
csvFile.close();
jsonFile.close();
}
应用程序可以正确读取csv文件的第一行,但无法继续直到结束,我不断收到此错误(即使我尝试将所有csv数据设置为一行:
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at CSVConverter.main(CSVConverter.java:17)
我意识到使用更具体的库会更简单,但由于我是Java的新手,我无法找到合适的软件包来下载和安装
答案 0 :(得分:1)
将close
语句移出while
循环(最好是finally
块)
csvFile.close();
jsonFile.close();
答案 1 :(得分:0)
请参阅以下内容,您的密切方法位于错误的位置。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewClass {
public static void main(String[] args) {
BufferedReader csvFile = null;
BufferedWriter jsonFile = null;
try {
csvFile = new BufferedReader(new FileReader("DataTextCsv.csv"));
jsonFile = new BufferedWriter(new FileWriter("converted.txt"));
String fileContent = csvFile.readLine();
// set the constant header
String jsonScript = "constant header of json content";
while (fileContent != null) {
fileContent = csvFile.readLine();
String[] tab = fileContent.split(",");
// variable content from csv file
jsonScript += "\"" + tab[0] + "\" :";
jsonScript += tab[1] + "," + "\n";
// End of json content construction
jsonScript = jsonScript.substring(0, jsonScript.length() - 2);
jsonScript += "}";
String[] tabWrite = jsonScript.split("\n");
for (String item : tabWrite) {
jsonFile.write(item);
jsonFile.newLine();
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
csvFile.close();
jsonFile.close();
} catch (IOException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
或者选项二使用try-with资源
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NewClass {
public static void main(String[] args) {
try (BufferedReader csvFile = new BufferedReader(new FileReader("DataTextCsv.csv")); BufferedWriter jsonFile = new BufferedWriter(new FileWriter("converted.txt"))) {
String fileContent = csvFile.readLine();
// set the constant header
String jsonScript = "constant header of json content";
while (fileContent != null) {
fileContent = csvFile.readLine();
String[] tab = fileContent.split(",");
// variable content from csv file
jsonScript += "\"" + tab[0] + "\" :";
jsonScript += tab[1] + "," + "\n";
// End of json content construction
jsonScript = jsonScript.substring(0, jsonScript.length() - 2);
jsonScript += "}";
String[] tabWrite = jsonScript.split("\n");
for (String item : tabWrite) {
jsonFile.write(item);
jsonFile.newLine();
}
}
csvFile.close();
jsonFile.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(NewClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
答案 2 :(得分:0)
您需要在while循环中为null
值添加另一个catch。此外,我不确定你是否打算像现在一样重复所有的行。这种方式只返回最后一行/整套数据
while (fileContent !=null){
fileContent=csvFile.readLine();
if (fileContent != null){
String[] tab = fileContent.split(",");
// variable content from csv file
jsonScript+="\""+tab[0]+"\" :";
jsonScript+=tab[1]+","+"\n";
// End of json content construction
jsonScript=jsonScript.substring(0,jsonScript.length()-2);
jsonScript+="}";
}else{
String[] tabWrite=jsonScript.split("\n");
for (String item:tabWrite){
result.append(item);
jsonFile.write(item);
jsonFile.newLine();
}
}
}
csvFile.close();
jsonFile.close();