在以前的項目中,我需要將文件內容讀入數組。現在我必須做同樣的事情,只有我必須將內容讀入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