Skip to content

Instantly share code, notes, and snippets.

@lucatk
Created August 31, 2014 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucatk/1f68475d7cfc6711a9ad to your computer and use it in GitHub Desktop.
Save lucatk/1f68475d7cfc6711a9ad to your computer and use it in GitHub Desktop.
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