在以前的项目中,我需要将文件内容读入数组。现在我必须做同样的事情,只有我必须将内容读入ArrayList。我遇到的一些问题是将文件内容读入ArrayList
如何逐步通过ArrayList分别添加每个项目?
如果文件包含超过10个输入,它必须退出。我尝试了以下方法,但无法正常工作。
代码:
public static String readFile(String fileName) throws IOException {
String result = "";
String line = "";
FileInputStream inputStream = new FileInputStream(fileName);
Scanner scanner = new Scanner(inputStream);
DataInputStream in = new DataInputStream(inputStream);
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
int lineCount = 0;
String[] numbers;
while ((line = bf.readLine()) != null) {
numbers = line.split(" ");
for (int i = 0; i < 10; i++) {
if(i > 10) {
System.out.println("The file you are accessing contains more than 10 input values. Please edit the file you wish to use so that it contains"
+ "> 10 input values. The program will now exit.");
System.exit(0);
}
matrix[i] = Integer.parseInt(numbers[i]);
}
lineCount++;
result += matrix;
}
我看不出有任何的ArrayList。 – 2012-01-16 21:31:12
什么是矩阵? – hmjd 2012-01-16 21:36:34
这是当前编程,我必须通过一个数组。我的问题是能够通过一个arrayList。我只是在展示我的背景思维过程。\ – fisherml 2012-01-16 21:41:26