各位大虾,我在写一个程序,用到了AWT,在程序中,我使用fileDialog打开一个文件,并每次都通过bufferedReader把文件的内容读取到textarea(在我的程序中这个textarea变量名为source)中。我希望在打开一个新的文件并把它的内容写到source中时,先把原来的内容清空。 我使用了 source.setText("");   当好像没有效果,每次都会在原来的基础上继续增加  大家帮帮我,看看是什么原因吧
谢谢谢谢~~  在线等~
class MyOpenFileListener implements ActionListener {
ScannerFrame sf = null; public MyOpenFileListener(ScannerFrame sf) {
this.sf = sf;
}

public void actionPerformed(ActionEvent arg0) {
FileDialog fd = new FileDialog(sf, "选择文件", FileDialog.LOAD);
fd.setVisible(true);
String path = fd.getDirectory() + fd.getFile();
System.out.print(path);
File file = new File(path);
int length;
System.out.println("i am used");
 source.setText("");就是这句话,好像没有效果。 source是我在外面定义的 try {
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
char buf[] = new char[1024];
String s = br.readLine();
while(s != null){
sb.append(s);
sb.append("\n");
s = br.readLine();
}
source.setText(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

解决方案 »

  1.   

    用flush()
    程序没看懂,不过br没有清楚干净,
      

  2.   

    我觉得这样是可以的,除非你别的地方有逻辑错误。还有一个办法,你不要把source定义成类变量,直接定义在这个事件处理的方法下面,定义成局部变量,反正你读文件也是在这个方法下面嘛,不要紧的。看看能不能解决你的问题。
      

  3.   

    source.setText("");后面加上sb = new StringBuffer();
      

  4.   

    谢谢 按照huoyin的方法解决了 谢谢各位了~~