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)
Related
I am currently working on a Java application in Intellij, and I cannot create a file within my artifact. As an example, I'm using File to create a file within the source, which is MainMenuData.txt.
File mainMenu = new File("MainMenuData.txt");
String absPath = mainMenu.getPath();
mainMenu.createNewFile();
BufferedReader br = new BufferedReader(new FileReader(absPath));
In this, I'm using File to make sure that file exists whenever it isn't.
Instead, I'd like to build within the (Production) artifact. Is that doable?
Anything helps. Thanks.
You could do that.
I assume your artifact is a jar file, which is nothing else than a zip file. You can obtain the location of your jar file in the file system and use the Zip File System to modify it. However I'm not sure, if the jvm might have a problem with that and it might not work on windows, since windows likes to block files, that are in use.
Also this would definitely fail, if your jar file is not stored locally.
A better approach would be to store your data files at the appropriate location in your system.
On Linux(and probably mac): <home>/.local/share/<your application name>/
On Linux(global): /var/lib/<application name>
On Windows(I think, better check it yourself): <appdata>/<your application name>
Your code would look something like this(for Linux):
File home = new File(System.getProperty("HOME");
File configDirectory = new File(home, ".local/share/<application name>");
configDirectory.mkdirs();
File mainMenu = new File(configDirectory, "MainMenuData.txt");
For windows do something similar. If you need both, you should check on which you are currently running.
I am making an application where I have to read text-files in order to make my program work. When I run this application in InteliJ it runs perfectly but when I make a .jar file I get a "no such file exception".
Can someone explain me how to access my files inside a .jar file from a class inside the same .jar file?
Can someone also tell me which type of path is more preferable to use (Absolute or relative)?
NOTE: the purpose of the program is to help friends to learn basic words in Arabic. This means that the .jar file will be moved from one computer to the other and it still has to work.
By the way, is putting everything into a .jar something good or are there better ways to put my project in one executable file?
Image of my directory structure
The code i use is:
Path path = Paths.get("src/Multimedia/Woordenschat/Boek1/Arabisch/Hoofdstuk 1.txt");
You can use a BufferedReader and get the file stream with getResourceAsStream():
InputStream in = getClass().getResourceAsStream("path");
BufferedReader input = new BufferedReader(new InputStreamReader(in));
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.
I have a problem, I want to list the files in "default" folder, this folder is in the resources folder :
-resources/languages/default/manyfiles
The second line throws a nullPointerException
InputStream in = getClass().getClassLoader().getResourceAsStream("languages/default/");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
It seems I can't do this with a folder but only with a file. The problem is I can't use File because this is inside a jar.
EDIT : here the content of the jar :
http://www.mediafire.com/view/05u5w20xupt1mo1/javapbfolder.bmp
There is no standard way to list the contents of a folder. The reason is that there concept of a file system behind the class resource loading, just names/location of a resource.
It depends what you want to do, how to work around this. Possible solutions come to my mind:
Retrieve the URL from one resource in the directory. Extract the JAR file from the URL and open the JAR via the ZipFile class. This is "hacky" and may or may not work, depending on the platform you run and security settings.
Generate an index file when you pack the JAR that contains the list of the resources.
Put in a JAR into the resource, that you then open via ZipFile.
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