如何读取JAR中的文本文件? [英] How to read a text file inside a JAR?

查看:115
本文介绍了如何读取JAR中的文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做的是在程序的JAR中存储一个文本文件(不会改变),以便可以读取它。文本文件的目的是它将被我的一个类读入,文本文件的内容将被添加到 JEditorPane 。该文件基本上是一个教程,当用户点击阅读教程的选项时,文件内容将被读取并显示在弹出的新窗口中。

What I am attempting to do is store a text file (that won't change) inside the JAR of the program so that it can be read. The purpose of the text file is that it will be read in by one of my classes and the contents of the text file will be added to an JEditorPane. The file will basically be a tutorial and when the user clicks on the option to read the tutorial, the file contents will be read and displayed in a new window that pops up.

我的GUI部分已经关闭了,但是为了将文件存储在JAR中以便可以访问它,我很遗憾。我已经读过使用 InputStream 会起作用,但在尝试了一些事情后我还没有让它工作。

I have the GUI portion of it down, but as far as storing the file in the JAR so it can be accessed, I am at a lost. I've read that using an InputStream will work, but after trying a few things I haven't gotten it to work yet.

我还将图像存储在JAR中,以用作GUI窗口的图标。这可以通过以下方式完成:

I also store images in the JAR to be used as icons for the GUI windows. This is accomplished with:

private Image icon = new ImageIcon(getClass()
    .getResource("resources/cricket.jpg")).getImage();

但是,这在尝试获取文件时不起作用:

But, this doesn't work when trying to get a file:

private File file = new File(getClass.getResource("resources/howto.txt"));

这是我现在的课程:

public class HowToScreen extends JFrame{

/**
 * 
 */
private static final long serialVersionUID = -3760362453964229085L;
private JEditorPane howtoScreen = new JEditorPane("text/html", "");
private Image icon = new ImageIcon(getClass().getResource("resources/cricket.jpg")).getImage();
private BufferedReader txtReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/howto.txt")));

public HowToScreen(){
    setSize(400,300);
    setLocation(500,200);
    setTitle("Daily Text Tutorial");
    setIconImage(icon);

    howtoScreen.setEditable(false);
    howtoScreen.setText(importFileStream());
    add(howtoScreen);
    setVisible(true);
}

public String importFile(){
    String text = "";
    File file = new File("howto.txt");
    Scanner in = null;

    try {
        in = new Scanner(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    while(in.hasNext()){
        text += in.nextLine();
    }

    in.close();
    return text;
}

public String importFileStream(){
    String text = "";
    Scanner in = new Scanner(txtReader);

    while(in.hasNext()){
        text += in.nextLine();
    }

    in.close();
    return text;
}
}

忽略 importFile 方法正在删除,有利于将教程文件存储在JAR中,使程序完全自包含,因为我限制程序可以使用多少空间。

Ignore the importFile method as that is being removed in favor of storing the tutorial file inside the JAR, making the program wholly self contained as I am limited to how much space the program can use.

编辑:
在尝试了下面的所有建议之后,我检查了我的JAR是否正在将文本文件打包在其中而事实并非如此。当用7zip打开JAR时,在我的资源文件夹中,我用于图标的图片就在那里,但不是文本文件。

After trying all of the suggestions below, I checked to see if my JAR is packaging the text file in it and it is not. When opening the JAR with 7zip, in my resources folder the picture I use for icons is there, but not the text file.

推荐答案

您不能在JAR文件中使用File。您需要使用InputStream来读取文本数据。

You cannot use File inside a JAR file. You need to use InputStream to read the text data.

BufferedReader txtReader = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/mytextfile.txt")));

// ... Use the buffered reader to read the text file.

这篇关于如何读取JAR中的文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆