0

When I read a file from the jar file and want to put it in in a jTextArea, it shows me crypted symbols, not the true content.

What I am doing:

public File loadReadme() {
    URL url = Main.class.getResource("/readme.txt");
    File file = null;

    try {
        JarURLConnection connection = (JarURLConnection) url
                .openConnection();
        file = new File(connection.getJarFileURL().toURI());

        if (file.exists()) {
            this.readme = file;
            System.out.println("all ok!");

        }
    } catch (Exception e) {
        System.out.println("not ok");
    }

    return file;
}

And then i read the file:

public ArrayList<String> readFileToArray(File file) {
    ArrayList<String> array = new ArrayList<String>();
    BufferedReader br = null;

    try {

        String sCurrentLine;
        br = new BufferedReader(new FileReader(file));
        while ((sCurrentLine = br.readLine()) != null) {
            String test = sCurrentLine;
            array.add(test);

        }

    } catch (IOException e) {
        System.out.println("not diese!");

    } finally {
        try {
            if (br != null)
                br.close();
        } catch (IOException ex) {
        }
    }
    return array;
}

Now, i put all lines from the ArrayList in the jTextArea, that showes me things like that:

PK����?����^��S?��3��� z_��
%�Q Tl?7��+�;� �fK� �N��:k�����]�Xk,������U"�����q��\����%�Q#4x�|[���o� S{��:�aG�*s g�'.}���n�X����5��q���hpu�H���W�9���h2��Q����#���@7(�@����F!��~��?����j�?\xA�/�Rr.�v�l�PK�bv�=

The textfiled contains:

SELECTION:
----------
By clicking the CTRL Key and the left mouse button you go in the selection mode.
Now, by moving the mouse, you paint a rectangle on the map.

DOWNLOAD:
---------
By clicking on the download button, you start the download.
The default location for the tiles to download is: <your home>

I am sure that the file exists! Does anyone know what the problem is? Is my "getResource" correct?

n00begon
  • 3,503
  • 3
  • 29
  • 42
Ueli Grueber
  • 57
  • 1
  • 5

3 Answers3

1

Based on the output, I'm suspecting your code actually reads the JAR file itself (since it starts with PK). Why not use the following code to read the text file:

Main.class.getResourceAsStream("/readme.txt")

That would give you an InputStream to the text file without doing the hassle of opening the JAR file, etc.

You can then pass the InputStream object to the readFileToArray method (instead of the File object) and use

br = new BufferedReader(new InputStreamReader(inputStream));

The rest of your code should not need any change.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
0

This seems to be an encoding problem. FileReader doesn't allow you to specify that. Try using

br = new BufferedReader(new InputStreamReader(new FileInputStream(file), yourEncoding));
Rahul
  • 44,383
  • 11
  • 84
  • 103
0

You seem to be making far too much work for yourself here. You start by calling getResource, which gives you a URL to the readme.txt entry inside your JAR file, but then you take that URL, determine the JAR file that it is pointing inside, then open that JAR file with a FileInputStream and read the whole JAR file.

You can instead simply call .openStream() on the original URL that getResource returned, and this will give you an InputStream from which you can read the content of readme.txt

br = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));

(if readme.txt is not encoded in UTF-8 then change that parameter as appropriate)

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183