How to convert text file into jar file.Actually In my java project I read and write data from a text file but when i convert my project into jar executable file then reading and writing from/into text file not working what should I do to make this work please help me.
-
2Is the text file within the Jar? If it is, you won't be able to write to. The file should be placed somewhere you Jar can find it (the current working directory for one of the OS's "application support" directory for another) – MadProgrammer Sep 23 '15 at 04:11
-
yes the text file is placed in the same folder – Umer Ali Sep 23 '15 at 04:12
-
1Same folder as the jar? Then, in most cases you should be able to access it by not supply a path to it (`new File("MyAwesomeTextFile.txt");`) – MadProgrammer Sep 23 '15 at 04:13
-
u didn't understand my Question in netbeans everything is working perfectly data is written ad read from text file perfectly but when i build my project and convert it into jar exe file then reading and writing not working – Umer Ali Sep 23 '15 at 04:15
-
1I understand, trust me, but since we have no code to understand what it is you are doing, we can only provide you with wide concepts of things that "should" work. If the file is the same directory as the Jar and NOT embedded within it, then you should be able to read/write to it just by supply the name to the file without any path, which will use the current working directory (which you can obtain through `System.getProperty("user.dir")`). If the file is embedded within the Jar, then you won't be able to write to it and will only be able to read from using `Class#getResource` – MadProgrammer Sep 23 '15 at 04:18
-
ok where i use this System.getProperty("user.dir")) in the main file or anywhere else? – Umer Ali Sep 23 '15 at 04:21
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90383/discussion-between-umer-ali-and-madprogrammer). – Umer Ali Sep 23 '15 at 04:22
-
You can make use of Classloader to read the file present in jar, but ofcourse you will not be able to write to it. To be able to read and write both, you may want to have a look at http://stackoverflow.com/questions/13000937/read-and-write-to-java-file-via-resource – Shikha Gupta Sep 23 '15 at 04:30
2 Answers
A jar file is immutable, for security reasons. You cannot save data back to a file stored in a jar file.
Data saved from a Java program must be stored outside the classpath. Where that is, is up to you.
If you program must ship with a sample/default file, to get started, you code can try reading the file from the external location. If not found there, read from the sample/default file in the jar file. After updating content in memory, save to external location, where it will be read from on next execution.

- 154,647
- 11
- 152
- 247
You can make use of Classloader to read the file present in jar, but ofcourse you will not be able to write to it.
To be able to read and write both file should be present in filesystem, you may want to have a look at Read and Write to Java file via Resource

- 1
- 1

- 103
- 1
- 7
-
-
@Andreas of-course it won't for a jar file. I have already mentioned that. – Shikha Gupta Sep 23 '15 at 04:57
-
I was explaining my down-vote. Question is *explicitly* about jar file, so an answer that explicitly *doesn't* work for jar files, is "not useful" (down-vote). --- Also, OP already got it working when not in a jar file, so lining to an article explaining how to do that is unnecessary, as in also "not useful". – Andreas Sep 23 '15 at 05:00