问题遇到的现象和发生背景
Java如何读取CSV文件的某一列然后存入stiring数组
Java如何读取CSV文件的某一列然后存入stiring数组
用BufferedReader按行读取文件内容,根据逗号对csv数据进行分割成数组。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import Java.util.Scanner;
public class ReadCsv {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("请输入要读取的列:");
int num=in.nextInt();
String path=“”;
File csv = new File(path); // CSV文件路径
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(csv));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line = "";
try {
List<String> allString = new ArrayList<>();
while ((line = br.readLine()) != null)
{
String []arr=line.split(",");
allString.add(arr[num+1]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}