获取文件的绝对路径

Get absolute path to File

所以我正在尝试使用 BufferedImages,因为它们更容易使用,而且加载它们似乎有点挑剔。我认为你需要绝对路径来加载它们,我试过只做 src/Package.image.png 但是当我导出到可运行的 jar 时它不起作用。此代码在 eclipse 中有效,但由于某种原因,当我将其导出为 .jar 时它不起作用。

package MainGame;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

 public class GraphicsPath {





public static String getGraphicsPath(){


    String path = null;

    PrintWriter writer = null;
    try {
        writer = new PrintWriter("text.txt");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    path = GraphicsPath.class.getProtectionDomain().getCodeSource().getLocation().getPath();  


    System.out.println(path);

    writer.println("orignal path    " + path);

    //FOR LINUX
    String[] splited = path.split("/");


    //FOR WINDOWS
    //String[] splited = path.split("\");
    writer.println(path);


    path = path.replace("%20", " ");

    path += "MainGame/";

    writer.println(path);
    writer.close();

    System.out.println(path);
    return path;

}

}

这是 text.txt 导出到 jar 时的内容

orignal path    ./
./
./MainGame/

这是 text.txt 的内容,同时仍在 java 项目中

orignal path    /home/mini/workspace/Pokemon%20Game/bin/
/home/mini/workspace/Pokemon%20Game/bin/
/home/mini/workspace/Pokemon Game/bin/MainGame/

您可以通过多种方式传递路径。请检查以下内容,也许其中之一适合您:

package test;

import java.io.File;
import java.io.IOException;

public class FilePaths {

    public static void main(String[] args) throws IOException {

        String[] fileArray = { 
                "C:\projects\workspace\testing\f1\f2\f3\file5.txt", 
                "folder/file3.txt",
                "../testing/file1.txt", 
                "../testing", 
                "f1/f2"
        };

        for (String f : fileArray) {
            displayInfo(f);
        }

    }

    public static void displayInfo(String f) throws IOException {
        File file = new File(f);
        System.out.println("========================================");
        System.out.println("          name:" + file.getName());
        System.out.println("  is directory:" + file.isDirectory());
        System.out.println("        exists:" + file.exists());
        System.out.println("          path:" + file.getPath());
        System.out.println(" absolute file:" + file.getAbsoluteFile());
        System.out.println(" absolute path:" + file.getAbsolutePath());
        System.out.println("canonical file:" + file.getCanonicalFile());
        System.out.println("canonical path:" + file.getCanonicalPath());
    }

}

有关文件路径的更多信息 here

让它有点太简单了,如果你把你的图像资源放在和GraphicsPath相同的目录下(都在class路径上),你可以做

BufferedImage image;
try (InputStream in = GraphicsPath.class.getResourceAsStream('my-image.png')) {
    if (in != null) {
        image = ImageIO.read(in );
    }
}

这就是你想要做的。如果您使用 EAR、WAR、JNLP、java -jar,或者,我很抱歉,小程序,玩路径可能甚至无法工作。在您知道存在的 class 旁边捆绑资源意味着您可以轻松访问这些资源。

现在,在您的情况下,它很笨重,因为它们不会为您复制到 bin 目录,因此您可能需要编辑用于开发的 class路径。

如果我没理解错的话,您有一个需要阅读的 package.image.png 文件,它位于 .jar 文件中。

我以前遇到过这个问题,我认为你不能使用路径来定位这个文件。在我的一个项目中,我实际上使用了 URL 来指定文件的位置。我会给你一个我做过的例子

所以在我的 jar 文件中,有一个 images 文件夹。说我有 /images/x.png

我这样做

URL u = this.getClass().getResource( "/images/x.png" );
a=ImageIcon(u);

我用它来创建 imageIcon,但我认为您可以检查如何使用 URL 读取文件。

编辑:我刚刚试过这个来读取文件,它有效。

URL test =Class.class.getResource( "/docs/test.txt" );
BufferedReader in = new BufferedReader(new InputStreamReader(test.openStream()));

所以你可以用其他东西包裹 InputStreamReader 来获取图像