Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 433
user2434692 Avatar asked Jun 06 '13 04:06

user2434692


2 Answers

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.
like image 110
Sri Harsha Chilakapati Avatar answered Sep 30 '22 15:09

Sri Harsha Chilakapati


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
}
like image 41
Paul Vargas Avatar answered Sep 30 '22 17:09

Paul Vargas