参考代码如下:
public class FileReaderForRead
{
public static void main(String [ ] args)
{
try {
System.out.println(System.in);
FileReader fileReader = new FileReader("D:\\import_users.txt");
BufferedReader buf = new BufferedReader(fileReader);
int i = 0;
String bufToString = "";
String readLine = "";
String[] myArray = new String[500]; //100:这个值你自己定义,但不宜过大,要根据你文件的大小了,或者文件的行数
while((readLine = buf.readLine()) != null){
myArray[i] = readLine;
i++;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
数组的动态性不好。建议先用List
public class $ {
public static void main(String[] args) {
Listdata = new ArrayList<>();
try {
Scanner in = new Scanner(new File("D:/a.txt"));
while (in.hasNextLine()) {
String str = in.nextLine();
data.add(str.split(" ,|"));
}
} catch (Exception e) {
e.printStackTrace();
}
// list转化为数组
String[][] result = data.toArray(new String[][] {});
for (String[] strings : result) {
for (String string : strings) {
System.out.print(string + " ");
}
System.out.println();
}
}
}