如何在没有文件选择器的情况下打开文件

时间:2012-03-17 13:38:59

标签: file filereader jmenuitem

我创建了一个“帮助”文本文件,当用户在没有打开文件选择器的情况下点击我的Java应用程序上的“帮助”时,我想打开该文件。我已将帮助文件保存在与我的代码相同的位置

我的尝试:

JTextArea open = new JTextArea ();
TabPane.add ("Help", open);
open.read (new FileReader (help.txt), null);

1 个答案:

答案 0 :(得分:0)

如果我读到你想要将文件的内容显示到textarea,对吧?

JTextArea open = new JTextArea();

BufferedInputStream inStream = new BufferedInputStream(this.getClass().getResourceAsStream("help.txt"));
    byte[] chars = new byte[1024];
    int bytesRead = 0;
    try {
        while( (bytesRead = inStream.read(chars)) > -1){
            open.append(new String(chars, 0, bytesRead));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

你可以这样做..

相关问题