我有一个显示文件内容的代码。我尝试使用2种不同的方法来做到这一点。但是,我没有用第一种方法得到答案,而在我用第二种方法得到了答案。我想知道y ans不是第一种情况?在第一种情况下,我使用了while循环来检查我的文件是否不为空...我没有得到文件的输出...在第二种情况下,我使用函数传递了相同的while循环,并且得到了结果
line++;
}
l = line;
System.out.println("lines are "+ l);
String[] text =new String[l];
for(i=0;i<l;i++)
{
text[i]=b.readLine();//Reads a line of text
System.out.println("text is "+text[i]);
//System.out.print("txt "+ text[i]);
}
return null;
}
}
输出:=行是4文本是null文本是null文本是null文本是null
代码2--
public class JavaApplication9 {
public static void main(String[] args) throws IOException {
// TODO code application logic here
new FileReader(path);ception ex) {
Logger.getLogger(ReadMe.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ReadMe.class.getName()).log(Level.SEVERE, null, ex);
} return ln;
}
}
输出-第4行我在这里获取文件的内容
好吧,对于#1,您有空值,因为
while((lin=b.readLine())!=null)
{
line++;
}
消耗了您所有的输入流。当你尝试过
text[i]=b.readLine();
您的阅读器已经在文件的末尾,因此没有其他要阅读的内容。
对于#2,您要声明两个FileReader,一个在rlh内,另一个在rlh之外。这就是为什么它似乎起作用的原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句