【问题标题】:How to open a file without a file chooser如何在没有文件选择器的情况下打开文件
【发布时间】:2012-03-17 13:38:58
【问题描述】:

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

我的尝试:

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

【问题讨论】:

    标签: file filereader jmenuitem


    【解决方案1】:

    如果我让你读到你想将文件的内容显示到文本区域,对吧?

    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();
        }
    

    你可以那样做..

    【讨论】:

    • 我不一定想在文本区域中显示它,只需打开文件,但我不确定调用该特定文件的代码。
    • 您好,它会读取您的 help.txt 文件并将结果写入 JTextArea.. this.getClass().getResourceAsStream("help.txt") 访问您的文件
    • 感谢,由于某种原因它没有获取 help.txt,即使它已保存在同一位置,但我已经制定了自己的解决方案,目前“相当”运行良好
    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 2017-10-19
    • 2012-04-07
    • 2017-04-14
    相关资源
    最近更新 更多