如何在应用程序终止之前将图像保存到磁盘?

How to save image to disk before application terminates?

我需要将像素阵列写入磁盘并在同一个应用程序中读回该文件。出于某种原因,文件在应用程序终止之前不会写入磁盘。 (只有这样,它们才会出现在保存它们的目录中)。我正在用 IntelliJ IDEA 编写这个应用程序,如果知道它有任何用处的话。

如何确保文件立即写入磁盘?这是我的代码:

protected void savePixelstoPNG(int[] pixels, String fileName) {
    BufferedImage image = new BufferedImage(getMapWidth(), getMapHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics graphics = image.getGraphics();

    for(int y = 0; y < getMapHeight(); y++) {
        for(int x = 0; x < getMapWidth(); x++) {
            graphics.setColor(new Color(pixels[x + y * getMapWidth()]));
            graphics.fillRect(x, y, 1, 1);
        }
    }

    try {
        File file = new File(fileName);
        ImageIO.write(image, "PNG", file);
    } catch(IOException e) {
        e.printStackTrace();
    }
}

编辑:我检查了文件夹,实际上它们正在立即写入磁盘。但是,在应用程序终止之前,这些更改不会反映在项目目录中(文件保存到 java 包中)。因此,当我在保存这些文件后(在相同的应用程序生命周期内)读取这些文件时,应用程序找不到这些文件,即使它们存在于磁盘上。

编辑 2:这是我用来使用相对目录路径从 class 路径读取文件的代码。初始资源从class路径读取。当它们被更新时,它们被写入 class 路径中的不同目录,因此原始资源不会被覆盖,因为原始资源应该在每次重新 运行 应用程序时被初始读取:

void myLoadMethod() {
        loadMapTiles("resource/tilemap_1-1.png");
        loadTriggerTiles("resource/triggermap_1-1.png");
}


protected void loadMapTiles(@NotNull String path) {
        URL url = getClass().getClassLoader().getResource(path);
        loadTiles(url, mapTiles);
}

protected void loadTriggerTiles(@NotNull String path) {
        URL url = getClaass().getClassLoader().getResource(path);
        loadTiles(url, triggerTiles);
}

protected void loadTiles(@NotNull URL url, @Nullable int[] dest) {
        try {
            System.out.println("Trying to load: " + url.toString() + "...");
            BufferedImage map = ImageIO.read(url);
            int[] pixels = new int[mapWidth * mapHeight];
            map.getRGB(0, 0, mapWidth, mapHeight, pixels, 0, mapWidth);
            System.arraycopy(pixels, 0, dest, 0, dest.length);
            System.out.println("Success!");
        } catch (IOException e) {
            System.out.println("failed...");
            e.printStackTrace();
        }
    }
}

请注意,mapTilestriggerTiles 是 class 中包含的字段,其中包含 loadMapTiles loadTriggerTilesloadTiles

您应该区分源文件和 class路径。

资源通常位于某种 src 目录中。例如。对于 Maven 项目,*.java 文件的标准是 src/main/java,其他资源 *.properties 的标准是 src/main/resources,等等

但后来 java 文件被编译并且 *.class 文件被复制到某个输出目录。例如。对于 Maven,这是 target/...,许多其他工具有 binoutput 作为已编译的 class 文件夹。资源保存在同一位置。

您似乎正在将文件写入 src 并尝试从其他位置读取它们(例如 Class#getResourceAsStream(path) 将从 classpath 读取数据)。你可以在下一个应用程序 运行 中读取文件这一事实让我认为在下一个构建中它被复制到输出目录并成为你的应用程序的 "visible"。

根据您的代码

URL url = getClass().getClassLoader().getResource(path);

构造一个 URL 到预期在 class 路径上的文件(很可能在 ${projectRoot}/target/classes/${path} 中)。

同时

File file = new File(fileName);

在相对于执行目录的路径中创建一个文件(最有可能相对于 ${projectRoot})。

通常 ClassLoader#getResource(path) 应该只用于在您的 class 路径上获取静态资源。

要在磁盘上构建路径,请使用 Paths#get(path)。如果你确实需要 URL,你可以这样做:

Paths.get(path).toURI().toURL();