3

In the code below, I am trying to copy the contents of src to dest and load dest to print the contents of it.

  1. 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.

  2. 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();
    }
}
timss
  • 9,982
  • 4
  • 34
  • 56
serverfaces
  • 1,155
  • 4
  • 22
  • 49

1 Answers1

1

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.

Vlad
  • 18,195
  • 4
  • 41
  • 71
  • For illustration, I used src/main/resources and my files are under /usr/local/tomcat/conf and they are not part of the package. – serverfaces Jan 13 '16 at 19:21
  • Follow up question: why are you using `getResourceAsStream()` on a file that's present in the filesystem, and not part of your program? Not sure it would work. – Vlad Jan 13 '16 at 19:25
  • I see what you're saying. I started off with src/main/resource and found this issue. Then I moved it under /usr/local/tomcat/conf, but forgot to change using getResourceAsStream. Coming back to my original question, why it didn't work when the file was under src/main/resources but the content of it is empty? – serverfaces Jan 13 '16 at 19:31
  • I found this while running a junit test case where I had the files under src/test/resources. – serverfaces Jan 13 '16 at 19:35
  • I still think it might be some copying and shadowing of resource files by the build tool? – Vlad Jan 13 '16 at 19:40
  • 1
    I have a standalone code (in the case of Junit whatever you see above was running under a method that was annotated with @Test). Interestingly I see the dest.txt file getting the content from src.txt (when I go refresh the dest.txt file), but the class loader failed loading it or something, printing nothing in the log. It happened only when dest.txt is empty. The same class-loader loaded it when the file had some content in it. – serverfaces Jan 13 '16 at 19:47