I have an image in my .jar (program.jar / resources / img / image.gif) and I would like to call the labels at the beginning of my main copy to copy them to the folder "C: \", It should check if there is whether the file is "C: \ folder \ image.gif" and, if not, copy it. I have researched this topic, but I cannot find a way to do this. This is what I have and it works ... but it creates an empty file (image.gif) whit 0 bytes. It creates a file, not a file from my .jar.
Code:
//MyResources.saveResource("/resources/img/image.gif", "C:\folder", "image.gif"); public static void saveResource(String fromFile, String toFolder, String toFile){ InputStream stream = MyResources.class.getResourceAsStream(fromFile); if (stream == null) { //send your exception or warning } OutputStream resStreamOut; int readBytes; byte[] buffer = new byte [4096]; try { resStreamOut = new FileOutputStream(new File (toFolder + File.separator + toFile)); while ((readBytes = stream.read(buffer)) != -1){ //LINE 80 resStreamOut.write (buffer, 0, readBytes); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
I also get an error (NullPointerException exception that explains why the files are empty, but I don't know how to solve it):
Exception in thread "main" java.lang.NullPointerException at com.github.CrafterPGSV.Chess.Library.MyResources.saveResource(MyResources.java:80) at com.github.CrafterPGSV.Chess.Library.MyResources.createFolders(MyResources.java:61) at com.github.CrafterPGSV.Chess.Chess.main(Chess.java:10)
My main calls: MyResources.createFolders (); "that create the folder if it does not exist. At the end of" createFolders "it calls" saveResources (...) "to save the image.
source share