Created
August 31, 2014 23:33
-
-
Save lucatk/1f68475d7cfc6711a9ad to your computer and use it in GitHub Desktop.
Save resource file to specific folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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