0

jarファイルからファイルを読み取り、それをjTextAreaに入れたい場合、実際のコンテンツではなく、暗号化されたシンボルが表示されます。

私がしていること:

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

そして、私はファイルを読みました:

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

ここで、ArrayListのすべての行をjTextAreaに配置します。これにより、次のように表示されます。

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

textfiledには次のものが含まれます。

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>

ファイルが存在すると確信しています!誰かが問題が何であるか知っていますか?私の「getResource」は正しいですか?

4

3 に答える 3

1

出力に基づいて、コードが実際にJARファイル自体を読み取っていると思われます(で始まるためPK)。次のコードを使用してテキストファイルを読み取ってみませんか。

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

これによりInputStream、JARファイルを開くなどの手間をかけずに、テキストファイルにアクセスできます。

次に、InputStreamオブジェクトを(オブジェクトではなく)readFileToArrayメソッドに渡して、File

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

コードの残りの部分は変更する必要はありません。

于 2013-03-27T10:01:06.277 に答える
0

これはエンコーディングの問題のようです。FileReaderそれを指定することはできません。使ってみてください

br = new BufferedReader(new InputStreamReader(new FileInputStream(file), yourEncoding));
于 2013-03-27T09:41:12.127 に答える
0

あなたはここであなた自身のためにあまりにも多くの仕事をしているようです。を呼び出すことから始めgetResourceます。これにより、JARファイル内のエントリへのURLが提供されreadme.txtますが、次にそのURLを取得し、内部を指しているJARファイルを判別してから、でそのJARファイルを開き、JARファイルFileInputStream全体を読み取ります。

代わりに、返さ.openStream()れた元のURLを呼び出すだけで、次のコンテンツを読み取ることができます。getResourceInputStreamreadme.txt

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

readme.txtUTF-8でエンコードされていない場合は、必要に応じてそのパラメーターを変更してください)

于 2013-03-27T11:29:25.587 に答える