Reputation: 1175
In the code below, I am trying to copy the contents of src to dest and load dest to print the contents of it.
If I have dest.txt available and it has some content in it, then I see that the contents gets overwritten and the contents gets printed too.
If the file dest.txt is not present, then it gets created, and the contents from src.txt are copied into it and the contents are printed as well.
The only issue is that if dest.txt is present and empty, then I see that contents from src.txt are copied into it, but nothing gets printed in the log.
I'm wondering why.
public static void main(String[] args) {
String resourcesPath = "src/main/resources/";
try
{
Path source = Paths.get(resourcesPath + "src.txt");
Path destination = Paths.get(resourcesPath + "dest.txt");
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("dest.txt");
StringWriter writer = new StringWriter();
IOUtils.copy(input, writer, "utf-8");
System.out.println("Properties file content is " + writer.toString());
}
catch (Exception e)
{
e.printStackTrace();
}
}
Upvotes: 3
Views: 386
Reputation: 18633
How are you packaging your app? If the file is already present in src/main/resources
, and you're creating a jar, then the empty file will be in the jar and getResourceAsStream()
might pick it up from the jar rather than the filesystem.
Upvotes: 1