I do have a Folder in my Ressources i want to extract to disk when the App is started the first time. I do have this peace of code here where I tried to copy them to disk, but all I get are empty files. The folder contains .gnh files. Where am I loosing my Bytes of the File?
public void getTemplates() throws URISyntaxException {
final URL url = TemplateUtils.class.getResource("/templates/");
if (url != null) {
final File dir = new File(url.toURI());
for (final File file : dir.listFiles()) {
try {
final OutputStream outStream = new FileOutputStream(
PathManager.INSTANCE.getRootPath() + file.getName());
final long writtenBytes = Files.copy(file.toPath(), outStream);
LOG.info(writtenBytes);
outStream.flush();
outStream.close();
} catch (final IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
}
the LOG.info(writtenBytes) says 0
EDIT: When I copy simple text Files everything is working fine. But with those .gnh Files nothing is working anymore. Is there another way to extract those Files to disk?
I got the solution: You need to create the File for the OutputStream first and then you can flush it.
final File path = new File(
PathManager.INSTANCE.getRootPath() + "templates");
path.mkdirs();
final File newFile = new File(path.toString()
+ File.separator + file.getName());
newFile.createNewFile();
final OutputStream outStream = new FileOutputStream(newFile);
Files.copy(file.toPath(), outStream);
outStream.flush();
outStream.close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.