简体   繁体   中英

Reading a Txt-File in a jar-File in Java

I've write a Java programm and packaged it the usual way in a jar-File - unfortunately is needs to read in a txt-File. Thats way the programm failed to start on other computer machines because it could not find the txt-file.

At the same time Im using many images in my programm but here there is no such problem: I "copy" the images to the eclipse home directory, so that they are packaged in the jar-File and usable through following command:

BufferedImage buffImage=ImageIO.read(ClassName.class.getClassLoader()
                    .getResourceAsStream("your/class/pathName/));

There is something similar for simple textfiles which then can be use as a normal new File() ?


Edit
Ive try to solve my problem with this solution:

package footballQuestioner;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.security.auth.login.Configuration;


public class attempter {

    public static void main(String[] args) {
        example ex = new example();
    }

}

class example  {



    public example() {

        String line = null;

        BufferedReader buff = new BufferedReader(new InputStreamReader(
                Configuration.class
                        .getResourceAsStream("footballQuestioner/BackUpFile")));

        do {
            try {
                line = buff.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } while (line != null);

    }

}

But it gives always an NullPointerException...do I have forgotten something?

Here is as required my file structure of my jar-File:

在此输入图像描述在此输入图像描述在此输入图像描述在此输入图像描述

You can load the file from the ClassPath by doing something like this:

ClassLoader cl = getClass().getClassLoader()
cl.getResourceAsStream("TextFile.txt");

this should also work:

getClass().getResourceAsStream(fileName);

File always points to a file in the filesystem, so I think you will have to deal with a stream.

There are no "files" in a jar but you can get your text file as a resource (URL) or as an InputStream. An InputStream can be passed into a Scanner which can help you read your file.

You state:

But it gives always an NullPointerException...do I have forgotten something?

It means that likely your resource path, "footballQuestioner/BackUpFile" is wrong. You need to start looking for the resource relative to your class files. You need to make sure to spell your file name and its extension correctly. Are you missing a .txt extension here?


Edit
What if you try simply:

BufferedReader buff = new BufferedReader(new InputStreamReader(
            Configuration.class.getResourceAsStream("BackUpFile")));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM