How to copy a file (image.gif) from my ".jar" to a folder?

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.

+4
source share
2 answers

The error was the first line of "saveResource ()":

 InputStream stream = this.getClass().getResourceAsStream(fromFile); 

Here, the "flow" has always been zero. Here's how to solve it (using Eclipse):

  • Create a new package in src (let him call it resources).

  • Create a new folder in the package (call me myPictures)

  • Create more folders if you want. I will create a new folder inside myPictures and I will name it partyPictures

  • Select one of your folders and place the images (or something else) inside. I will have an image called imageName.jpg inside partyPictures

  • Then go to "Navigator", find your Proyect, and you will see that there is a folder called bin. Inside you should see your packages organized in folders.

  • Look for the folder inside the basket that has the name of the previously created package (I will have a folder with resources). Inside the folder, you will see a tree view of the folders inside the package (those that you created earlier).

  • Search for your files. (I will search for the resources /myPictures/partyPictures/imageName.jpg)

  • If it does not exist, InputStream stream = this.getClass (). getResourceAsStream (fromFile); will always be null. To fix this, copy the image (or any other file) to the folder.

  • Then use

     String fromFile = "/resources/myPictures/partyPictures/imageName.jpg"; InputStream stream = this.getClass().getResourceAsStream(fromFile); 
  • Change the fromFile line to your file path and always start with / . Your code will look like this:

String fromFile = "/ package_name / folder_name / file_name.instance ";

InputStream stream = this.getClass (). getResourceAsStream (fromFile);

It should be!

0
source

I don't know why the other answer got the deletion, but you need resStreamOut.close() after you finish writing.

You need to implement the case where stream == null (suppose an exception is thrown).

In addition, never break or swallow errors: printStackTrace() does not work with an error, the error is ignored.

0
source

All Articles