readline读到文件结尾时返回值是不同的,你看一下doc就知道了!

解决方案 »

  1.   

    当读到文件结尾时readLine()返回null;
    String line;
    while((line = reader.readLine()) != null){
        System.out.println(line);
    }
    就可以按原样读出整个文件
      

  2.   

    readLine 每执行一次  ,就会 向下 一行null就是结束了!
      

  3.   

    楼上的都有道理。你也可以用监控文件的长度来完成。
    这一种方法当你对RandomAccessFile写入随意数据时。
    要读出来的时候,控制它的长度,往往很有效。
    以下的程序段是我在处理一个特定问题的一部分~~
    但愿对你有帮助raf = new RandomAccessFile(file,"rw");
    actual = raf.length();
    if(actual == 0)  {  println("NONEWS");
       return;
    }
    while((line = raf.readUTF())!= null) {
       writer.println(line);
       monitor = raf.getFilePointer();
       if(actual == monitor + 2) break;
       ch = raf.read();
       ch = raf.read();
    }
      

  4.   

    那读二进制文件呢,如果byte b[]=new byte[4];
    f.read(b);
      

  5.   

    你去看看帮助啊,每一种方法的返回值都有说明,
    If no byte is available because the stream is at end of file, the value -1 is returned
      

  6.   

    String r=null;
    File f=new File("a.txt");
    while((r=f.readLine())!=null){
      ....
     }
      

  7.   

    readline我知道了
    byte b[]=new byte[4];
    f.read(b);呢?
    感觉不太好设置循环呀
      

  8.   

    File fileIn=new File(strFilePath);
            String out = DateDealer.nowStringLong().toString();
            BufferedReader bfr = null;
            logger.info("正在读取文件" + strFilePath);
            try
            {
                if(fileIn.exists())
                {
                    //get BufferedReader from the file
                    bfr=new BufferedReader(new FileReader(fileIn));
                    //read one line of the file and make a String
                    String strResult;
                    while((strResult = bfr.readLine()) != null)
                    {                    //call insertData to insert the data in string
                        //insertData(strResult);                }
                    bfr.close();
                }
            }
            catch(DException ex)
            {
                logger.error("文件格式不正确");
                throw ex;
    }