import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;public class ReadFile { public void readTxtFile(){
String filePath = "C:/Users/Administrator/Desktop/新建文件夹/1.txt";  try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ 
    InputStreamReader read = new InputStreamReader(
     new FileInputStream(file),encoding);
    BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
String a = "-1";
    while((lineTxt = bufferedReader.readLine()) != null){
     System.out.println(lineTxt);
     if ((注解:这里判断是否读完)) {
     System.out.println("调用无限循环Timing");
     }
    }
    bufferedReader.close();
    read.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ReadFile rf = new ReadFile();
rf.readTxtFile();
}
}

解决方案 »

  1.   

      while((lineTxt = bufferedReader.readLine()) != null)你这句就是了啊,读到文件结束这个条件就是false了
      

  2.   

    楼上说的对,为空null既是读取完。
      

  3.   

    readLine()方法如果读取到末尾,会返回Null
      

  4.   

    String s = null;
    while((s=bufferedReader.readLine())!=null)
      

  5.   

    while((lineTxt = bufferedReader.readLine()) != null)  已经作个判断了。
      

  6.   

    bufferedReader.readLine()) != null   
      

  7.   

    教你个更简单的方法:for ( String line = null ; ( line = reader.readLine() ) != null ;  ) {
       System.out.println( line );
    }
      

  8.   

    直接api文档,朋友。
    readLine
    public String readLine()
                    throws IOException读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。 返回:
    包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 
    抛出: 
    IOException - 如果发生 I/O 错误
      

  9.   

    BufferedReader - readLineReturns:  A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
      

  10.   

    条件已经写在while里面了,不需要再来if判断了
      

  11.   

    你的while((line =br.readLine()) !=null)已经做了判断了,不需要自己在判断了
      

  12.   

     readLinepublic String readLine()
                    throws IOException    读取一个文本行。通过下列字符之一即可认为某行已终止:换行 ('\n')、回车 ('\r') 或回车后直接跟着换行。    返回:
            包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null 
        抛出:
            IOException - 如果发生 I/O 错误参考:https://piterlin.github.io/doc/jdk6_cn/java/io/BufferedReader.html