CodeGuy CodeGuy - 1 year ago 91
Java Question

Read and Write to Java file via Resource

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?

Answer Source

Try below:

     InputStreamReader isReader= 
                      new InputStreamReader(
      BufferedReader br = new BufferedReader(isReader);     


     InputStreamReader isReader= 
                      new InputStreamReader(
      BufferedReader br = new BufferedReader(isReader);   

For output, try below:

      PrintWriter writer = 
               new PrintWriter(
                     new File(this.getClass().getResource(templateName).getPath());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download