Mofty
Mofty

Reputation: 414

How to get a .txt File into a jar and read it

I'm trying to get a .txt file into a jar.

I tried to put it in to the package i'm using.

This will lead to the result, that i can't use the following code:

 double[] test1 = ReadFile.readfile("Daten8B.txt");

Is there an other way to generate a jar with the .txt in it?

If not, how do i get the path of the .txt?

note:

to get the path i tried:

String pattern = "Daten8B.txt";
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);

the output here was:

sun.nio.fs.WindowsFileSystem$2@15db9742

Upvotes: 1

Views: 157

Answers (2)

Vollmilchbb
Vollmilchbb

Reputation: 481

You should consider using getResourceAsStream("/a.txt") method to get your file. An example would look like

InputStream inputStream = Main.class.getResourceAsStream("/Daten8B.txt");

Your txt file should be in your source folder for this example.

Instead of Main you can use any class in your jar-file.

Upvotes: 2

Amila
Amila

Reputation: 5213

You should place your text file into the resource directory. Then you can access it with:

this.getClass().getResourceAsStream("Daten8B.txt");

Upvotes: 1

Related Questions