25

I'm trying to read and write to a file but I'd like to access that file via Resource.

This is what I do

File f = new File(ClassLoader.getSystemResource("/blah/blah/Properties.prop").toURI());
BufferedReader br = new BufferedReader(new FileReader(f));
String line = br.readLine();

PrintWriter p = new PrintWriter(new File(ClassLoader.getSystemResource("/blah/blah/Properties.prop").toURI()));

but neither seems correct. What is the correct way to do this?

CodeGuy
  • 28,427
  • 76
  • 200
  • 317

3 Answers3

27

For input, try below:

     InputStreamReader isReader = 
                      new InputStreamReader(
                          this.getClass().getResourceAsStream(templateName));
      BufferedReader br = new BufferedReader(isReader);     

or

     InputStreamReader isReader = 
                      new InputStreamReader(
                          <youclassName>.class.getResourceAsStream(templateName));
      BufferedReader br = new BufferedReader(isReader);   

For output, try below:

      PrintWriter writer = 
               new PrintWriter(
                     new File(this.getClass().getResource(templateName).getPath()));
Rabhi salim
  • 486
  • 5
  • 17
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • Should work same way, but don't read and write the same file at same time. – Yogendra Singh Oct 21 '12 at 18:55
  • can you show me code. I can't seem to get the PrintWriter working – CodeGuy Oct 21 '12 at 18:56
  • Sorry I missed. `getResourceAsStream` is always input stream. Updated the answer for PrintWriter, please try. By the way, did reader work? – Yogendra Singh Oct 21 '12 at 19:06
  • 1
    It should be mentioned that there may not be a file that can be written to. If the resource is part of a `.jar` file, writing will fail. (Even if you could modify the jar file, it could be signed or simply not be located on the machine running the jvm...) – fabian Jun 01 '18 at 15:16
3

All solution above show how you can receive access to your resources in build folder. It means that resources have been moved to build directory with .class files.
If you need to write some files into resources for saving them after terminated program, then you will have to describe path starting with root project:

./src/main/resources/foo.ext

This solving generally not recommended but may be necessary in other cases.

Dmytro Melnychuk
  • 2,285
  • 21
  • 23
1

More simply,

 try (BufferedReader bufferedReader = Files.newBufferedReader(Paths.get(Resources.getResource("path").toURI()))) {
        //bufferedReader...
    }
    try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(Resources.getResource("path").toURI()))) {
        //bufferedWriter...
    }

Note that the path is relative to the resources folder.

chaiyachaiya
  • 2,617
  • 18
  • 19
  • 1
    This solution doesn't appear to work when the resource is embedded within a jar file. In my case the resource's URI evaluates to `"jar:file:/C:/PCProxAPI/PCProxAPI.jar!/com/agilysys/rguest/buy/peripherals/PCProxPlusConfig.cfg"` and `Paths.get(...)` throws `java.nio.file.FileSystemNotFoundException` – HairOfTheDog May 10 '16 at 19:08
  • 6
    For anyone else who is wondering that `Resources` class is actually `com.google.common.io.Resources` – HairOfTheDog May 11 '16 at 16:41
  • @HairOfTheDog, "Paths" utility only "knows" the file system. Once a file is packaged in the jar, it's no more a file system paradigm. – chaiyachaiya May 20 '16 at 15:38
  • @HairOfTheDog: We can think to smthg like : Files.streamToString(Resources.getResource("foo/bar.txt").openStream()); It will work even if the file is packaged (exception was thrown by Paths.get method) – chaiyachaiya May 20 '16 at 15:47
  • I tried this but it does not create the outputstream in Resources folder but in target folder. – dardy Feb 21 '17 at 14:03