Include text file in Eclipse JAR export? - java

I am using a FileInputStream to read from a File object. My program is able to read the text file when run in the Eclipse IDE, but not when it is run as a JAR file.
I am exporting it as a JAR file and not a Runnable JAR file, and I know that the text file is already included in the JAR file because I extracted it to check.
This is what my file structure looks like:

I would suggest you to put your learnaboutfonts.txt inside src folder(not inside package) and read file in source code like the following:
Resource resource = new ClassPathXmlApplicationContext().getResource("classpath:learnaboutfonts.txt");
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
And you can use br as you want to use. Happy Coding.

You should use something like:
InputStream input = getClass().getResourceAsStream("/learnaboutfonts.txt");
Note that the trick is using the leading "/" properly. If you don't put the leading /, then it searches for the file in the package of the class its invoked from.

How do you access your file? You maybe need a relative file path like "./mytext.txt").
Use this pattern:
InputStream is = new getClass().getResourceAsStream(
"/text.txt");
Test to include your text-file in your package of your class where you want to access the file.

Related

How to open a file independently of the path of the Java project?

I'm working with Eclipse and there, I have Java Project with the name "Test" which also contains text files. A class in this Project should be able to read in one of these files with a BufferedReader. This is my current code for that:
BufferedReader in = new BufferedReader(new FileReader("C:/Users/workspace/Test/testFile.txt"));
My file is always in the Project, but when I move the Project to another path, the file path changes, too, so I have to adjust the code with the new path.
I dont't want that, because it's impractical, so what can I do? How can I get the path of the Project?
You can add file to resources folder and read like
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("file/test.xml").getFile());
Try something like this:
File currentDirFile = new File(".");
String fileDir = currentDirFile.getAbsolutePath();

How do you read a text file within the project when running the project from a jar?

I'm working on a Java program in Netbeans that I'd like to be able to
run from an external jar file
And I cannot seem to read from a file that isn't located inside of
the project default directory or some subfolder located there, and
that only works inside Net beans.
What I'd like to be able to do is read the text file from the file
path
src/assets/files/textFile.txt
.
I've tried all of the suggestions here, but they don't seem to work for me. Here's the code I'm currently using:
File file = new File("assets/files/textFile.txt");
if (!file.exists()) {
throw new FileNotFoundException("File does not exist");
}
FileReader fr = new FileReader(file.getAbsoluteFile());
BufferedReader br = new BufferedReader(fr);
When I try to run this, the exception is thrown each time.
The term for such non-Files is resource.
String encoding = "UTF-8";
new BufferedReader(
new InputStreamReader(YourClass.getResourceAsStream("/assets/files/textFile.txt"),
encoding));
Also FileReader is an old utility class that uses the default system encoding. That might be different from where the text file was created, hence Unicode, such as encoded in UTF-8, would be ideal. For Windows users you might write a BOM character at the beginning to mark the file as UTF-8: "\ufeff".
The class YourClass should be from the jar. Here an absolute path is used "/...". A relative path would start with the package path.
Also Windows is not case-sensitive. Other systems and the zip format of the jar are case-sensitive. Check the jar with a zip utility for correct paths.

File cannot be accessed after export (Java)

I know that there are many questions like this out there, but so far there have been none that have been of help.
In eclipse, I have a file inside of my project folder ,and I can get it to load using:
BufferedReader in = new BufferedReader(new FileReader(new File(path)));
When I export the project it will not load the file because it cannot find the file. I have no idea what is going on. Any suggestions? Thanks.
If you want to read the file, you have two options.
You Could...
Make sure that the file is in the same directory as the exported jar file and/or the same execution context. This will allow you to use something like BufferedReader in = new BufferedReader(new FileReader(new File("./" + fileName)));
When referencing the file, it need to specify a relative path to the file.
This means you must ensure that the file is copied to the correct location when ever you move the jar file.
You Could...
Embed the file within the jar as an embedded resource. This means that the file becomes part of the jar file. This also means that you can no longer reference it as a File, but need to use Class#getResource or Class#getResourceAsStream, for example...
BufferedReader in = new BufferedReader(
new InputStreamReader(this.getResourceAsStream("/path/to/resource")));
In order to use this, the file must be placed within the resources directory within your Eclipse project and the directory included within your build path
If you want to be able to write to the file, then you MUST use option one. Embedded resources can not be written to (without a lot of work)

Access file from another package

My situation is as following : I have package packA where I have classA, and I have a file.txt in packB.resources. In classA I'm using this to access file.txt :
InputStreamReader in = new InputStreamReader(new FileInputStream("/packB/resources/file.txt"), "UTF-8");
But unfortunately it shows me an exception :
java.io.FileNotFoundException : \packB\resources\file.txt (The
specified path was not found)
The FileInputStream class opens a file in the file system based on a file system path.
But what you are apparently trying to do is to open a resource located via the classpath. You should be using Class.getResourceAsStream(String).
If your file.txt is packaged with application you should not access using file system at all. The application may be packaged into jar, so the file is not located in file system. You should access it as a resource instead:
InputStreamReader in = new InputStreamReader(getClass().getResourceAsStream("/packB/resources/file.txt"), "UTF-8"));
Use something like the following. Note, the / used as a prefix before the package name. YourClass is assumed to be in packA.
InputStream stream = YourClass.class.getResourceAsStream("/packB/resources/file.txt");
You should remove the trailing / from the file path and use classA.class.getClassLoader().getResourceAsStream("packB/resources/file.txt").

Eclipse-Java: where to put file for Read purpose

I'm programming Java in Eclipse IDE. Here is code I want to read file:
File file = new File("file.txt");
reader = new BufferedReader(new FileReader(file));
I put file.txt in two place:
1) same folder of this SOURCE file.
2) in bin\...\ (same folder of this CLASS file)
But I allways receive NO FILE FOUND.
Please help me.
thanks :)
If the file ships with your application, it would be better accessed as a resource than as a file. Simply copy it to somewhere in your build path and use Class.getResourceAsStream or ClassLoader.getResourceAsStream. That way you'll also be able to access it if you bundle your app as a jar file.
Currently, you're looking for the file relative to the process's current working directory, which could be entirely unrelated to where the class files are.
if you put the file under sources and inside the package "test" for example, the path is:
./src/test/file.txt
you can use
File file = new File("./src/test/file.txt");
System.out.println(file.exists());
The path ./bin/test/file.txt will work in the second case and is more suitable for a normal java project

Categories