自己写了段代码有关打开和保存一个文件的问题,编译成功,运行问题也不大!但是出现了个问题是,程序只能读取文件的一部份!请高手帮忙改下,代码如下!import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class testrw2 extends WindowAdapter implements ActionListener{                     Frame f;
   Button o,b;
   String name;
   TextArea ta;
   
public static void main(String[] args) 
{        new testrw2(args[0]);}    
   public testrw2(String name)
{      
      this.name=name;   f=new Frame("testrw");
  f.addWindowListener(this);
   b=new Button("保存文件");
  o=new Button("打开文件");
  o.setActionCommand("open");
  b.setActionCommand("save");
  o.addActionListener(this);
  b.addActionListener(this);
  ta=new TextArea(30,40);
  f.add(ta,BorderLayout.SOUTH);
  f.add(b,BorderLayout.CENTER);
  f.add(o,BorderLayout.NORTH);
  f.pack();
  f.setVisible(true);
}
        public void actionPerformed(ActionEvent e)
{      String cmd=e.getActionCommand();
         try
{     if(cmd.equals("open"))

{
      FileReader fr=new FileReader(name);
    LineNumberReader flir=new LineNumberReader(fr);
String str=flir.readLine();
while (str != null)
{
 ta.setText(str);
 str=flir.readLine();}
 flir.close();
     fr.close();
}
                 else
{
                 
FileWriter fo=new FileWriter(name);
  
 fo.write( ta.getText());
 fo.close();
}

}
catch (IOException IOe)
{       System.out.println(e);
} }     public void windowClosing(WindowEvent e)
{System.exit(0);}                
}

解决方案 »

  1.   


        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            try {
                if (cmd.equals("open")) {
                    FileReader fr = new FileReader(name);
                    LineNumberReader flir=new LineNumberReader(fr);
                    String str = null;
                    StringBuffer strBuff = new StringBuffer();
                    while ((str =flir.readLine()) != null) {
                        strBuff.append(str).append("\n");
                    }
                    ta.setText(strBuff.toString());
                    flir.close();
                    fr.close();
                } else {
                    FileWriter fo = new FileWriter(name);
                    fo.write(ta.getText());
                    fo.close();
                }
            } catch (IOException IOe) {
                System.out.println(e);
            }
        }
      

  2.   

    也可以直接调用TextArea类的append(String str)就ok了,你setText()方法只设置了文件内容最后一行显示在TextArea中
      

  3.   

    楼上用StringBuffer的已经给出了解法了, 问题就在ta.setText(str);
    while (str != null) { 
      ta.setText(str);      //你每次都重新赋值了, 改为ta.setText(ta.getText() + str); 但是还是楼上的解法更好.
      str=flir.readLine();
      

  4.   

    3楼的方法确实还不错,但我今天发现个问题,当那个程序读取*.doc文件时为什么变成乱码了呢?有什么方法解决没有?
      

  5.   

    那个不是普通文件,读取Word,Excel要用到poi的.
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import org.apache.poi.hwpf.extractor.WordExtractor;    File file = new File("你的doc文件");
        try {
          FileInputStream fis = new FileInputStream(file);
          WordExtractor wordExtractor = new WordExtractor(fis);
             System.out.println(wordExtractor.getText())//输出文件内容
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
         e.printStackTrace();
       }
      }
    }
      

  6.   

    String str=null;
    StringBuffer value=new StringBuffer("");
    while ((str=flir.readLine()) != null)
    {
    value.append(str);
    }
    flir.close();
        fr.close();
        ta.setText(str);
    }