Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to copy a file (image.gif) from my ".jar" to a folder?
    primarykey
    data
    text
    <p>I've an image in my .jar (program.jar/resources/img/image.gif) and I would want to invoke a methot at the the beggining of my main to copy them to "C:\folder". It should check if the file "C:\folder\image.gif" exist, and, if not, copy it. I've researched about this topic, but I can't find a way to do it. This is what I have, and it works... but it creates an empty file (image.gif) whit 0 bytes. It creates <strong>a</strong> file, not <strong>the</strong> file from my .jar.</p> <h3>Code:</h3> <pre><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(); } } </code></pre> <p>I also get an error (a NullPointerException that explains why the files are empty, but I don't know how o solve): </p> <pre><code>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) </code></pre> <p>My main calls "MyResources.createFolders();" which create the folder if it doesn't exist. At the end of "createFolders", it calls "saveResources(...)" to save the image.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload