0

I have a .jar file and at root of them located a folder. How I can read this folder into java File class?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
  • What does it mean to "read a folder into the File class?" – Ernest Friedman-Hill Jun 03 '13 at 13:33
  • jar is just a zip with another name and a defined structure - try the zip-streams. – Mirco Jun 03 '13 at 13:34
  • @ErnestFriedman-Hill updated question title. I need to get directory from root of jar into `File` object. I.e. I want to access to directory in root of jar using Java `File` API – WelcomeTo Jun 03 '13 at 13:34
  • 1
    You seem to be looking for http://stackoverflow.com/questions/1386809/copy-directory-from-a-jar-file – devnull Jun 03 '13 at 13:35
  • 1
    File is a wrapper for a file or directory on the file system. It can't be used to access something in a jar file. You should explain us what you want to do with this directory in the jar file. You should also tell us if the jar file is in the classpath or not. – JB Nizet Jun 03 '13 at 13:44

1 Answers1

1

You can't use File, since this file does not exist independently on the file system. Instead you need getResourceAsStream() to get the contents of the folder, like this:

InputStream in = getClass().getResourceAsStream("/FILENAME.TXT");
BufferedReader input = new BufferedReader(new InputStreamReader(in));
Omar Wagih
  • 8,504
  • 7
  • 59
  • 75