Lama
Lama

Reputation: 2974

getResourceAsStream() finds resource, but it will write an empty file

I have a .properties File located in my resource folder. I would like to load the file but when I'm using IOUtils to read the content of my file, it will just create a new empty file and won' copy the content of my test.properties.

    try (InputStream demoTemplate = 
          MyClass.class
         .getClassLoader()
         .getResourceAsStream("/config/test.properties")
    {
        IOUtils.copy(demoTemplate, new FileWriter(destinationFile));
        LOGGER.debug(" demo file content: " + OUtils.toString(demoTemplate));
    }

My logging output is empty, the File will be created but it has no content.

When I change the path of the test.properties file it will cause an FileNotFound exception so I'm sure the ClassLoader can load the respurce. The test.properties File contains data.

Upvotes: 2

Views: 659

Answers (1)

Paul Bilnoski
Paul Bilnoski

Reputation: 556

Your logging output is empty because you have exhausted the InputStream into the destination file.

Your output could be empty because the FileWriter was never flushed or closed properly.

Perhaps you could read the destination file and log that, open the original source again, keep a copy of the input stream's contents, or log as you transfer the data, though that would require not using the simple IOUtils helper.

Upvotes: 2

Related Questions