如何将文件(image.gif)从我的“.jar”复制到文件夹?

时间:2021-06-11 16:03:40

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 a file, not the file from my .jar.

我在我的.jar(program.jar / resources / img / image.gif)中有一个图像,我想在我的main的开头调用methot将它们复制到“C:\ folder”。它应检查文件“C:\ folder \ image.gif”是否存在,如果不存在,则复制它。我研究了这个话题,但我找不到办法。这就是我所拥有的,它的工作原理......但它创建了一个空文件(image.gif),字母为0字节。它创建一个文件,而不是我的.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 (a NullPointerException that explains why the files are empty, but I don't know how o solve):

我也得到一个错误(NullPointerException解释了为什么文件是空的,但我不知道如何解决):

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();" which create the folder if it doesn't exist. At the end of "createFolders", it calls "saveResources(...)" to save the image.

我的主要调用“MyResources.createFolders();”如果文件夹不存在,则创建该文件夹。在“createFolders”结束时,它调用“saveResources(...)”来保存图像。

2 个解决方案

#1


0  

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

不知道为什么其他答案被删除了,但是你写完后需要resStreamOut.close()。

You need to implement the case when stream == null (throw exception presumably).

你需要在stream == null(大概抛出异常)时实现这种情况。

Also, never catch and swallow errors: printStackTrace() is not error handling, it is error ignoring.

此外,永远不会捕获和吞下错误:printStackTrace()不是错误处理,它是错误忽略。

#2


0  

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

错误是“saveResource()”的第一行:

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

Here, "stream" was always null. Here is the way to solve this (using Eclipse):

这里,“stream”始终为null。以下是解决此问题的方法(使用Eclipse):

  1. Create a new package in src (let's call it resources).

    在src中创建一个新包(让我们称之为资源)。

  2. Create a new folder in your package (let's call it myPictures)

    在包中创建一个新文件夹(让我们称之为myPictures)

  3. Create more folders if you want. I'll create a new folder inside myPictures and I'll call it partyPictures

    如果需要,创建更多文件夹。我将在myPictures中创建一个新文件夹,我将其称为partyPictures

  4. Choose one of your folders and put the images (or whatever) inside. I'll have an image called imageName.jpg inside partyPictures

    选择一个文件夹并将图像(或其他)放入其中。我将在partyPictures中有一个名为imageName.jpg的图像

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

    然后,转到“导航器”,找到您的Proyect,您将看到有一个名为bin的文件夹。在里面,您应该看到您的包在文件夹中组织。

  6. Look for the folder inside bin that has the name of the package you created before (I'll have a folder called resources). Inside the folder you'll see a tree-view of the folders inside the package (the ones you created before).

    查找bin中具有您之前创建的包名称的文件夹(我将有一个名为resources的文件夹)。在文件夹中,您将看到包内的文件夹(您之前创建的文件夹)的树视图。

  7. Look for your files. (I'll look for resources/myPictures/partyPictures/imageName.jpg)

    寻找你的文件。 (我会寻找资源/ myPictures / partyPictures / imageName.jpg)

  8. If it doesn't exist, InputStream stream = this.getClass().getResourceAsStream(fromFile); will always null. To fix this, copy the image (or whatever file) to the folder.

    如果它不存在,则InputStream stream = this.getClass()。getResourceAsStream(fromFile);永远是空的。要解决此问题,请将图像(或任何文件)复制到该文件夹​​。

  9. Then use

    String fromFile = "/resources/myPictures/partyPictures/imageName.jpg";
    InputStream stream = this.getClass().getResourceAsStream(fromFile);
    
  10. 然后使用String fromFile =“/ resourcess / myPictures / partyPictures / imageName.jpg”; InputStream stream = this.getClass()。getResourceAsStream(fromFile);

  11. Change the String fromFile to your file's path, and always start with a /. Your code will look like this:

    将String fromFile更改为文件的路径,并始终以/开头。您的代码将如下所示:

String fromFile = "/packageName/ folderName/ fileName.extension";

字符串fromFile =“/ packageName / folderName / fileName.extension”;

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

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

That should be it!

#1


0  

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

不知道为什么其他答案被删除了,但是你写完后需要resStreamOut.close()。

You need to implement the case when stream == null (throw exception presumably).

你需要在stream == null(大概抛出异常)时实现这种情况。

Also, never catch and swallow errors: printStackTrace() is not error handling, it is error ignoring.

此外,永远不会捕获和吞下错误:printStackTrace()不是错误处理,它是错误忽略。

#2


0  

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

错误是“saveResource()”的第一行:

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

Here, "stream" was always null. Here is the way to solve this (using Eclipse):

这里,“stream”始终为null。以下是解决此问题的方法(使用Eclipse):

  1. Create a new package in src (let's call it resources).

    在src中创建一个新包(让我们称之为资源)。

  2. Create a new folder in your package (let's call it myPictures)

    在包中创建一个新文件夹(让我们称之为myPictures)

  3. Create more folders if you want. I'll create a new folder inside myPictures and I'll call it partyPictures

    如果需要,创建更多文件夹。我将在myPictures中创建一个新文件夹,我将其称为partyPictures

  4. Choose one of your folders and put the images (or whatever) inside. I'll have an image called imageName.jpg inside partyPictures

    选择一个文件夹并将图像(或其他)放入其中。我将在partyPictures中有一个名为imageName.jpg的图像

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

    然后,转到“导航器”,找到您的Proyect,您将看到有一个名为bin的文件夹。在里面,您应该看到您的包在文件夹中组织。

  6. Look for the folder inside bin that has the name of the package you created before (I'll have a folder called resources). Inside the folder you'll see a tree-view of the folders inside the package (the ones you created before).

    查找bin中具有您之前创建的包名称的文件夹(我将有一个名为resources的文件夹)。在文件夹中,您将看到包内的文件夹(您之前创建的文件夹)的树视图。

  7. Look for your files. (I'll look for resources/myPictures/partyPictures/imageName.jpg)

    寻找你的文件。 (我会寻找资源/ myPictures / partyPictures / imageName.jpg)

  8. If it doesn't exist, InputStream stream = this.getClass().getResourceAsStream(fromFile); will always null. To fix this, copy the image (or whatever file) to the folder.

    如果它不存在,则InputStream stream = this.getClass()。getResourceAsStream(fromFile);永远是空的。要解决此问题,请将图像(或任何文件)复制到该文件夹​​。

  9. Then use

    String fromFile = "/resources/myPictures/partyPictures/imageName.jpg";
    InputStream stream = this.getClass().getResourceAsStream(fromFile);
    
  10. 然后使用String fromFile =“/ resourcess / myPictures / partyPictures / imageName.jpg”; InputStream stream = this.getClass()。getResourceAsStream(fromFile);

  11. Change the String fromFile to your file's path, and always start with a /. Your code will look like this:

    将String fromFile更改为文件的路径,并始终以/开头。您的代码将如下所示:

String fromFile = "/packageName/ folderName/ fileName.extension";

字符串fromFile =“/ packageName / folderName / fileName.extension”;

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

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

That should be it!