Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Save resource file to specific folder
public boolean saveResourceToPath(String resourcePath, String targetFolder) {
InputStream in = getResource(resourcePath);
if(in == null)
return false;
int lastIndex = resourcePath.lastIndexOf('/');
File outDir = new File(dataFolder, resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0));
if(!outDir.exists())
outDir.mkdirs();
File outFile = new File(targetFolder, resourcePath)
try {
if (!outFile.exists() || replace) {
OutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
} else {
return false;
}
} catch (IOException ex) {
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.