I created a class called FileCopyFromJAR that has a static method called copy. This allows me to copy files to outside of the JAR by using getResourceAsStream:
public static void copy(String source,String dest) throws IOException{
try{
File sourceFile = new File(source);
File destFile = new File(dest);
InputStream in = FileCopyFromJAR.class.getResourceAsStream(source);
OutputStream out = new FileOutputStream(destFile);
int bufferSize = 1024;
byte[] buf = new byte[bufferSize];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf,0,len);
}
in.close();
out.close();
}
catch(IOException e){
throw e;
}
}
Inside my code, I will call this by doing something like (this assumes test.txt is in the root of my .jar file):
FileCopyFromJAR.copy("/test.txt","c:\\test.txt");
However, I get a FileNotFoundException if I specify a folder or a file inside a folder. Both of these return an error:
FileCopyFromJAR.copy("folder\test.txt","c:\\test.txt");
FileCopyFromJAR.copy("folder", "c:\\folder");
I've also tried using various combinations like /folder\test.txt, etc. but nothing seems to work. Is there a way to make this work or do I have to use a different method?