如何在Java的jar文件夹中访问许多资源映像?

时间:2020-07-08 17:41:37

标签: java jar directory

我有一个用于加载蓝色图像的类,它在Eclipse中可以正常工作,但在导出的jar中却不能。如何在不知道图像名称的情况下访问名为“ blue”的文件夹(目录)中的所有蓝色图像?

public class Blue
{
   public static void read() throws Exception
   {
      File directoryBlueImages = new File(
            Blue.class.getResource("blue").getFile());
      String[] blueImages = directoryBlueImages.list();
      List<BufferedImage> blueImagesList = new ArrayList<>();
      for (String blueImage : java.util.Objects.requireNonNull(blueImages))
      {
         blueImagesList.add(ImageIO
               .read(Blue.class.getResourceAsStream("blue/" + blueImage)));
      }
      ApplicationImages.setBlueImages(blueImagesList);
   }
}

更新

我已经尝试过了,但是它也不起作用。我收到NullPointer异常。我尝试了“ / blue”和“ blue”,甚至是“ .blue”。

import java.awt.image.BufferedImage;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import javax.imageio.ImageIO;

import vokabeltrainer.ApplicationImages;

public class Blue
{
   public static void read() throws Exception
   {
      List<BufferedImage> blueImagesList = new ArrayList<>();

      try (Stream<Path> pathStream = Files.walk(Paths.get(Blue.class
            .getClassLoader().getResource("blue").toURI().toURL().getPath()))
            .filter(Files::isRegularFile))
      {
         for (Path file : (Iterable<Path>) pathStream::iterator)
         {
            blueImagesList.add(ImageIO
                  .read(Blue.class.getResourceAsStream(file.toString())));
            ;
         }
      }

      ApplicationImages.setBlueImages(blueImagesList);
   }
}

1 个答案:

答案 0 :(得分:0)

我改编了How to list the files inside a JAR file?的答案

首先,我要区分是从jar还是从Eclipse运行:

  try
  {
     Blue.readZip(); // when inside jar
  }
  catch (Exception e)
  {
     try
     {
        Blue.read(); // during development
     }
     catch (Exception e1)
     {
        System.out.println("Could not read blue.");
        e1.printStackTrace();
     }
  }

然后,Blue类如下所示:

public class Blue
{
   private static List<BufferedImage> blueImagesList = new ArrayList<>();

   public static void read() throws Exception
   {
      File directoryBlueImages = new File(
            Blue.class.getResource("blue").getFile());
      String[] blueImages = directoryBlueImages.list();

      for (String blueImage : java.util.Objects.requireNonNull(blueImages))
      {

         blueImagesList.add(ImageIO
               .read(Blue.class.getResourceAsStream("blue/" + blueImage)));

      }
      ApplicationImages.setBlueImages(blueImagesList);
   }

   public static void readZip() throws Exception
   {
      CodeSource src = Blue.class.getProtectionDomain().getCodeSource();
      if (src != null)
      {
         URL jar = src.getLocation();
         ZipFile zipFile = new ZipFile(jar.getFile());
         ZipInputStream zip = new ZipInputStream(jar.openStream());
         while (true)
         {
            ZipEntry ze = zip.getNextEntry();
            if (ze == null)
               break;
            String name = ze.getName();
            if (name.startsWith("vokabeltrainer/resources/blue/"))
            {
               blueImagesList.add(ImageIO.read(zipFile.getInputStream(ze)));
            }
         }
      }
      else
      {
         throw new IOException("can not find code source for blue images");
      }
      ApplicationImages.setBlueImages(blueImagesList);
   }
}
相关问题