java 逐行读取txt中的数据存放到数组

2024-11-18 03:47:58
推荐回答(1个)
回答(1):

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {
public static void main(String[] args) throws NumberFormatException,
IOException {
BufferedReader reader = new BufferedReader(new FileReader("file.txt"));
String line = "";
List list = new ArrayList();
while ((line = reader.readLine()) != null) {
list.add(Integer.parseInt(line));
}
reader.close();
Integer[] num = list.toArray(new Integer[list.size()]);
System.out.println(Arrays.toString(num));
}
}