读取Demo:
public static void main(String[] args) {
File csv = new File("C:\\Result.csv"); // CSV文件路径
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(csv));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line = "";
String everyLine = "";
try {
List<String> allString = new ArrayList<>();
while ((line = br.readLine()) != null) //读取到的内容给line变量
{
everyLine = line;
System.out.println(everyLine);
allString.add(everyLine);
}
System.out.println("csv表格中所有行数:" + allString.size());
} catch (IOException e) {
e.printStackTrace();
}
}
写入Demo:
public static void main(String[] args) {
try {
File csv = new File("C://writers.csv");//CSV文件
BufferedWriter bw = new BufferedWriter(new FileWriter(csv, true));
//新增一行数据
bw.newLine();
bw.write("abc" + "," + "123" + "," + "1234");
bw.close();
} catch (FileNotFoundException e) {
//捕获File对象生成时的异常
e.printStackTrace();
} catch (IOException e) {
//捕获BufferedWriter对象关闭时的异常
e.printStackTrace();
}
}