所以我在将一个文本文件读入我的程序时遇到了问题.这是代码:
try{
InputStream fis=new FileInputStream(targetsFile);
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
//while(br.readLine()!=null){
for(int i=0;i<100;i++){
String[] words=br.readLine().split(" ");
int targetX=Integer.parseInt(words[0]);
int targetY=Integer.parseInt(words[1]);
int targetW=Integer.parseInt(words[2]);
int targetH=Integer.parseInt(words[3]);
int targetHits=Integer.parseInt(words[4]);
Target a=new Target(targetX, targetY, targetW, targetH, targetHits);
targets.add(a);
}
br.close();
}
catch(Exception e){
System.err.println("Error: Target File Cannot Be Read");
}
Run Code Online (Sandbox Code Playgroud)
我正在读取的文件是100行参数.如果我使用for循环它完美地工作.如果我使用while语句(for循环上面注释掉的那个),它会在50处停止.用户可能会运行带有任意行数的文件的程序,因此我当前的for循环实现赢了工作.
为什么线路while(br.readLine()!=null)停在50?我检查了文本文件,没有任何东西可以挂起来.
当我使用while循环时,我没有从try-catch中得到任何错误,所以我很难过.有人有主意吗?