我想从一个文本文件中读取其中的内容,代码如下``但是有个缺点,就是当那个文件结束了,还是会追加一个"\n"
于是我就加个注释部分的判断,但郁闷的是,要是文本的最后一行只是空字符(不知道是不是空字符,就是打完一行之后,回车,下一行什么也不写)就不读了
本来这不影响整个文本的读取``,个人觉得总有点不完美,恳请各位大大帮忙```
还有一个问题就是是read()快还是readline()快?
reader = new BufferedReader(new FileReader(new File(1.txt)));
text.setText("");
                                        String tem;
                                        JTextArea text=new JTextArea();
tem = reader.readLine();
while (tem !=null) {
      text.append(tem);
      text.append("\n");
      tem = reader.readLine(); 
                                               //if((tem = reader.readLine())!=null)text.append("\n");  1 }
reader.close();

解决方案 »

  1.   

     reader = new BufferedReader(new FileReader(new File(1.txt)));
                        text.setText("");
                                            String tem;
                                            JTextArea text=new JTextArea();
                        tem = reader.readLine();
                        text.append(tem);
                        while (tem = reader.readLine())!=null) 
                              text.append("\n"+tem);

                                                 
                        reader.close();代码帮你改了,应该可以满足你的要求,个人认为readline()比较快,毕竟它自带了一个缓冲区
      

  2.   

    当然是readLine()更快拉,它是读取一整行
    而read()是一个一个的读取,
    import java.awt.*;
    import java.io.*;public class Reader {
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    File file = new File("C:/Text.txt");
    try {
    FileReader fr = new FileReader(file);
    BufferedReader bread = new BufferedReader(fr);
    String str = null;
    while ((str = bread.readLine())!= null) 
    {
    System.out.print(str);
    }
    bread.close();
    fr.close();

    catch (IOException w) {
    }
    }}
      

  3.   

    readline()快,看了一下你的程序,帮你改了一下:
    reader = new BufferedReader(new FileReader(new File(1.txt)));
    String text="";
    String tem="";
    while(null!=(tem=in.readLine()))
    {
      text.append(tem);
      text.append("\n");
    }
    is.close();看看是不是你想要的结果!
      

  4.   

    readline()快,看了一下你的程序,帮你改了一下: 
    reader   =   new   BufferedReader(new   FileReader(new   File(1.txt))); 
    String   text=""; 
    String   tem=""; 
    while(null!=(tem=reader.readLine())) 

        text.append(tem); 
        text.append("\n"); 

    is.close(); 看看是不是你想要的结果!
      

  5.   

    哦,我是按行读的,每读一行加一个回车,你把text.append("\n");   
    去掉试试看,这是我用的最好用的方法了,别的我也不会了,
      

  6.   

    没太明白你的意思
    猜一下吧
        reader = new BufferedReader(new FileReader(new File(1.txt)));
                        text.setText("");
                                            String tem;
                                            JTextArea text=new JTextArea();
                        tem = reader.readLine();
                        while (tem !=null) {
                              text.append(tem);
                              tem = reader.readLine(); 
                              if(tem!=null)text.append("\n");
                        }
                        reader.close();
      

  7.   

    看了一下readLine的函数说明,因为readLine把回车换行符都丢掉了,恐怕不能满足你的要求
    用read就可以了,read不会比readLine慢。
      

  8.   

    谢谢,我用read()是可以了,不过不知道会不会比readline慢`还有,为什么read()读到行末,会读到"\r"和"\n"的?
      

  9.   

    绝对正确的版本
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    public class BufferedReaderTest { /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    BufferedReader reader=null;
    try{
    reader = new BufferedReader(new FileReader(new File("1.txt")));
            StringBuffer tem = new StringBuffer();
            String text=new String();
            int LEN=1024;
            char[] buf = new char[LEN];
            int count=0;
            while ((count=reader.read(buf,0,LEN))!=-1) {
                  tem.append(buf,0,count);
             }
            text=tem.toString();
            reader.close();
            System.out.print(text);
    }catch(Exception ex)
    {

    }
    }}你改一下就可以用在你的程序中了。
    要改的就是JTextArea 的值只有在最后设置,刚开始放在StringBuffer中
    我觉得read的速度会比readLine更快,因为我一次读取1024个字节,显然比你一行要长,不过没有做测试验证。
      

  10.   

    read没有行末的概念,它把所有的字符都读取了,包括"\r"和"\n"
    LEN的大小可以自己设,太大太小都不好,1024或者2048可能比较好。
    用StringBuffer可以稍微提高速度
      

  11.   

    谢谢楼上的大大``
    用你的方法基本行了
    不过照你做了,每行还是会读到"\r\n",在textarea里要按两个才能删掉每行的最后一个``
    我将所有的"\r\n"替换为"\n"就行了```,可能是我刚才问得不够清楚,我是问,为什么会读到"\r\n"而不是读到
    一个换行符"\n"?
      

  12.   

    哦,那是在windows下面编辑文本文件的时候,默认回车换行都有,就是"\r\n"
    稍好一点的文本编辑器都可以把文本文件存为unix格式,这样就只有"\n"了。这种文件你如果用记事本打开会发现没有换行。
    这就是所谓的硬回车和软回车。
    你自己做编辑器的话,保存的时候注意一下,读取的时候就不需要替换了。
      

  13.   

    newflypig,你的程序读文本文件基本没问题,但是你没有明白楼主的意思,
    他的意思是:
    第一行
    第二行(不回车)
    --你的程序处理这个没有问题
    还有一种
    第一行
    第二行(回车)
    (光标到了这里)
    你的程序读取这两种文件会显示相同的结果,就是第一种。
    楼主自己写的程序就是只会显示成第二种。
    我上面已经说了因为readLine把回车换行丢掉了,所以会出现上面的问题。
    只有用read才能满足楼主的要求