如何在文本区中显示java文本文件?

lin_zhlie 2001-05-10 10:02:00
我在编一个用户界面的文本编辑器

部分代码如下:
public void actionPerformed(ActionEvent evt)
{
String arg = evt.getActionCommand();
// if(arg.equals("New")
if(arg.equals("Open"))
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileFilter(new FileFilter()
{
public boolean accept(File f)
{
return f.getName()
.toLowerCase()
.endsWith(".java")
||f.isDirectory();
}
public String getDescription()
{
return "Java Files";
}
});
int r = chooser.showOpenDialog(this);
if (r == JFileChooser.APPROVE_OPTION)
{
String fileName = chooser.getSelectedFile()
.getName();

loadFile(fileName);

}
}


if(arg.equals("Exit"))
System.exit(0);
}
public void loadFile(String name)
{
try{

//name 为通过JChooser对话框得到的文件名
FileInputStream fs = new FileInputStream(name);
//text 为JTextArea的变量
//text 先清空
text.setText("");

BufferedReader in = new BufferedReader(new
InputStreamReader(fs));

String s;
while ((s = in.readLine()) != null)
//向文本区写入
text.append(s+"\n");
fs.close();
}
catch(IOException e)
{
}
}

可是我通过菜单open选择文本文件后
文本区tex仍然是空白的
着如何改正?
...全文
103 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
hello_wyq 2001-05-10
  • 打赏
  • 举报
回复
while ((s = in.readLine()) != null)
//向文本区写入
text.append(s+"\n");
fs.close();
}
把fs.close()拿出来!

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧