我不是開放試圖讀取與內容 一個簡單的文本文件input.txt中文件中的Java
Line 1
Line 2
Line 3
但它總是轉到異常並顯示錯誤。
import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[]){
List<String> text = new ArrayList<String>();
try{
BufferedReader reader = new BufferedReader(new FileReader("input.txt"));
for (String line; (line = reader.readLine()) != null;) {
text.add(line);
}
System.out.println(text.size()); //print how many lines read in
reader.close();
}catch(IOException e){
System.out.println("ERROR");
}
}
}
使用Eclipse作爲我的IDE,如果這有所作爲的話。我試過這個代碼http://www.compileonline.com/compile_java_online.php 它運行良好,爲什麼不運行在Eclipse中?
嘗試打印出異常。然後你就會知道錯誤究竟是什麼。 – iamnotmaynard
我猜'FileNotFoundException - 但是,你永遠不應該忽略異常本身。 –
同意。可能的input.txt不在正確的文件夾中。 – Cogman