I am writing an application that needs to copy some static assets from its resources directory. The directory structure is like this;
java/
resources/
assets/
file1.txt
file2.txt
subdir/
file3.txt
I want to copy the assets directory (recursively) to a location on the file system. If I use IOUtils.copy(InputStream in, OutputStream out)
then I get a null pointer exception.
InputStream in = getClass().getResourceAsStream("/assets");
OutputStream out = new FileOutputStream(new File("/path/to/write/to"));
IOUtils.copy(in, out);
Exception in thread "main" java.lang.NullPointerException
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2146)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2102)
at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:2123)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:2078)
However, if I change the input and output stream so they are files and not directories, then it copies fine.
Is there a way to recursively copy a directory that is part of the jar to the file system?