• Post Reply Bookmark Topic Watch Topic
  • New Topic

File IO and Jar file  RSS feed

 
Parshuram Walunjkar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am using FileIO. And creating FileReader Object in Java file. as like
FileReader aa = new FileReader ("prop.txt");
Giving relative path.
If I compile and then make an executables jar (Adding all class files and prop.txt file).
And run it then it gives the exception FileNotFoundException.
How can I give path in FileReader constructor?
Waiting for reply,
Regards from
Parshuram Walunjkar
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Parshuram,
If you want to read a file that is packaged in a JAR file, you need to use the "java.util.jar.JarFile" class, it contains the following method:
public ZipEntry getEntry(String name)
and (I imagine) you would pass "prop.txt" as the parameter.
You can then get an "InputStream" for the "prop.txt" file by invoking:
public InputStream getInputStream(ZipEntry ze)
I suggest you study the documentation ("javadoc") for the "JarFile" class.
Hope this has helped you.
Good Luck,
Avi.
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out Class.getResource() and Class.getResourceAsStream().
getResourceAsStream() is usually easier because it skips a step and returns an InputStream. Then you can use that input stream to read through your file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!