【发布时间】:2017-04-10 13:16:40
【问题描述】:
我已经编写了以下代码来读取文件:
package com.test.application;
import java.io.*;
import java.io.IOException;
public class FileRead {
public static void main(String[] args) {
try{
File file=new File("Hello.txt");
FileReader fileReader=new FileReader(file);
BufferedReader reader=new BufferedReader(fileReader);
/*String line=null;
while((line = reader.readLine())!=null){
System.out.println(line);
} */
System.out.println("This is using no string variable!!!");
while(reader.readLine()!=null){
System.out.println(reader.readLine());
}
reader.close();
}
catch(IOException e){
}
}
}
我的文本文件是:
What is Lorem Ipsum?
Lorem Ipsum is simply dummy text of the printing and typesetting
industry.
Lorem Ipsum has been the industry's standard dummy text ever since the
1500s, when an unknown printer took a galley of type and scrambled it
to make a type specimen book.
It has survived not only five centuries, but also the leap into
electronic typesetting, remaining essentially unchanged.
It was popularised in the 1960s with the release of Letraset sheets
containing Lorem Ipsum passages, and more recently with desktop
publishing software like Aldus PageMaker including versions of Lorem
Ipsum.
我的问题是当我使用字符串变量从文件中读取时,我会获取文件的所有内容,即:
String line=null;
while((line = reader.readLine())!=null){
System.out.println(line);
}
但是,当我使用下面的代码 sn-p 读取文件时,跳过了几行,并且没有读取整个文件。
while(reader.readLine()!=null){
System.out.println(reader.readLine());
}
谁能解释一下为什么会这样。
【问题讨论】: