我试图将csv文件"read_ex.csv"读入数组.我在web/stackoverflow上无休止地搜索,找到一种方法将文件读入数组.我能做的最好的是以流式方式读取它,但由于文件的大小可变,我无法将其存储在数组中.我相信ArrayList是一种处理可变大小数组的方法,但我不知道如何使用它.基本上我希望能够在while循环结束后访问String数组"values".
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
public class sortarray {
public static void main (String []agrs){
String fileName= "read_ex.csv";
File file= new File(fileName);
try{
Scanner inputStream= new Scanner(file);
while(inputStream.hasNext()){
String data= inputStream.next();
String[] values = data.split(",");
System.out.println(values[1]);
}
inputStream.close();
}catch (FileNotFoundException e) {
e.printStackTrace();
}
//This prints out the working directory
System.out.println("Present Project Directory : "+ System.getProperty("user.dir"));
}
}
Run Code Online (Sandbox Code Playgroud)