Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

File IO and Jar file  RSS feed

 
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
 
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.
 
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!