各位大虾,我在写一个程序,用到了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();
}
谢谢谢谢~~ 在线等~
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();
}
程序没看懂,不过br没有清楚干净,