How to read a text file inside a JAR? [duplicate]

為{幸葍}努か 提交于 2019-11-28 03:44:34

问题


This question already has an answer here:

  • Java resource as file 6 answers

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.

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.

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"));

Here is my Class as it is now:

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

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.

EDIT: 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.


回答1:


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.



回答2:


Try the next (with the full path package):

InputStream inputStream = ClassLoader.getSystemClassLoader().
        getSystemResourceAsStream("com/company/resources/howto.txt");
InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader in = new BufferedReader(streamReader);

for (String line; (line = in.readLine()) != null;) {
    // do something with the line
}



回答3:


You code will not compile. Class.getResource() returns a URL, and File has no constructor with a URL as an argument.

You can just use .getResourceAsStream() instead, it returns an InputStream directly, you just have to read the contents of the file from that stream.

Note: both of these methods return null if the resource is not found: don't forget to check for that...




回答4:


the contents of the text file will be added to an JEditorPane.

See DocumentVewer & especially JEditorPane.setPage(URL).

Since the help is an embedded-resource it will be necessary to gain an URL using getResource(String) as detailed in the info. page.

.. tried this: URL url = this.getClass().getResource("resources/howto.txt");

Change:

URL url = this.getClass().getResource("resources/howto.txt");

To:

URL url = this.getClass().getResource("/resources/howto.txt");  // note leading '/'


来源:https://stackoverflow.com/questions/16953897/how-to-read-a-text-file-inside-a-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!