文件读取:获取部分输出
我有以下代码来从我的文件中检索数据。当我执行代码时,我发现它只给出了总行数的 50%。为什么会这样?
public static void main(String args[]) throws IOException
{
int count = 1;
try {
FileInputStream fileInput = new FileInputStream("C:/FaceProv.log");
DataInputStream dataInput = new DataInputStream(fileInput);
InputStreamReader inputStr = new InputStreamReader(dataInput);
BufferedReader bufRead = new BufferedReader(inputStr);
while(bufRead.readLine() != null)
{
System.out.println("Count "+count+" : "+bufRead.readLine());
count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
i have the following code to retrive the data from my file. When i execute the code i come to know it's giving only 50% of the lines from the total lines. Why it's happening ?
public static void main(String args[]) throws IOException
{
int count = 1;
try {
FileInputStream fileInput = new FileInputStream("C:/FaceProv.log");
DataInputStream dataInput = new DataInputStream(fileInput);
InputStreamReader inputStr = new InputStreamReader(dataInput);
BufferedReader bufRead = new BufferedReader(inputStr);
while(bufRead.readLine() != null)
{
System.out.println("Count "+count+" : "+bufRead.readLine());
count++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你读了两行:
但你只数了一次。因此,您实际上正在读取整个文件,但只计算了一半的行。
将其更改为:
看看会发生什么。
You're reading the lines twice:
but you only count them once. So you're actually reading the whole file, but counting only half of the lines.
Change it to:
and see what happens.
因为
丢弃了刚刚读取的行。
Because
discards the line it just read.