73

I have a file that is in my classpath, e.g. com/path/to/file.txt. I need to load or reference this file as a java.io.File object. The is because I need to access the file using java.io.RandomAccessFile (the file is large, and I need to seek to a certain byte offset). Is this possible? The constructors for RandomAccessFile require a File instance or String (path).

If there's another solution to seek to a certain byte offset and read the line, I'm open to that as well.

zb226
  • 9,586
  • 6
  • 49
  • 79
jake
  • 1,405
  • 3
  • 19
  • 33
  • Working end to end example is here for details: [https://stackoverflow.com/questions/1464291/how-to-really-read-text-file-from-classpath-in-java/49120666#49120666] – ravibeli Mar 05 '18 at 22:43

4 Answers4

232

Try getting hold of a URL for your classpath resource:

URL url = this.getClass().getResource("/com/path/to/file.txt")

Then create a file using the constructor that accepts a URI:

File file = new File(url.toURI());
joelittlejohn
  • 11,665
  • 2
  • 41
  • 54
48

This also works, and doesn't require a /path/to/file URI conversion. If the file is on the classpath, this will find it.

File currFile = new File(getClass().getClassLoader().getResource("the_file.txt").getFile());
Hotel
  • 1,361
  • 11
  • 13
  • 1
    This worked for me whereas this did not: URL url = this.getClass().getResource("/com/path/to/file.txt"). Was this because the file I was loading was in a folder called "resource" rather than being in a package/folder shared by "this.getClass()"? Anyway, thanks. – thebiggestlebowski Jun 30 '15 at 20:13
  • 1
    Really concise answer. – Harihar Das Jan 25 '16 at 11:52
  • 3
    This wont work if the resource path (or a parent path of a relative resource) contains spaces. getFile() returns a URL encoded string with %20s that the File(String) constructor interprets literally. @joelittlejohn answer below will correctly handle this case as it passes a URI to the `File` constructor – dan carter Jul 05 '16 at 23:28
21

I find this one-line code as most efficient and useful:

File file = new File(ClassLoader.getSystemResource("com/path/to/file.txt").getFile());

Works like a charm.

Gondy
  • 4,925
  • 4
  • 40
  • 46
  • This worked for me and is the cleanest answer on this page. – Jeremy Feb 05 '16 at 13:12
  • 7
    This wont work if the resource path (or a parent path of a relative resource) contains spaces. getFile() returns a URL encoded string with %20s that the File(String) constructor interprets literally. @joelittlejohn answer below will correctly handle this case as it passes a URI to the File constructor. Should be `File file = new File(ClassLoader.getSystemResource("com/path/to/file.txt").toURI());` – dan carter Jul 05 '16 at 23:34
5

Or use directly the InputStream of the resource using the absolute CLASSPATH path (starting with the / slash character):


getClass().getResourceAsStream("/com/path/to/file.txt");

Or relative CLASSPATH path (when the class you are writing is in the same Java package as the resource file itself, i.e. com.path.to):


getClass().getResourceAsStream("file.txt");

Jiri Patera
  • 3,140
  • 1
  • 20
  • 14
  • And how would this allow the original poster to use java.io.RandomAccessFile? – joelittlejohn Dec 06 '10 at 13:12
  • Your answer is more proper, I admit, and that is why I voted it up. One can skip to a certain offset in a file using the `java.io.InputStream.skip(long)` method and then read a line, for example, by using the `java.io.BufferedReader.readLine()` method. The question was also aimed at other solutions than just the `RandomAccessFile` one. – Jiri Patera Dec 06 '10 at 14:44