Java 如何从我的项目(编写代码的地方)获取文件

Java 如何从我的项目(编写代码的地方)获取文件,java,file,path,eclipse-plugin,project,Java,File,Path,Eclipse Plugin,Project,我正在尝试从我的项目文件夹中获取文件(readme.txt)。不知道如何获得项目的位置。当我说project时,我指的是编写应用程序代码的位置,而不是运行时应用程序。我试着得到绝对路径,相对路径。。。它总是给我运行时应用程序的文件夹。还尝试了类似.getClass()的操作,并尝试提取path或System.getProperty(“user.dir”)。这两个还为我提供了eclipse…/…运行时应用程序的路径。我正在制作eclipse插件,这个文件应该是我插件的一部分,所以当用户点击按钮时,

我正在尝试从我的项目文件夹中获取文件(readme.txt)。不知道如何获得项目的位置。当我说project时,我指的是编写应用程序代码的位置,而不是运行时应用程序。我试着得到绝对路径,相对路径。。。它总是给我运行时应用程序的文件夹。还尝试了类似.getClass()的操作,并尝试提取path或System.getProperty(“user.dir”)。这两个还为我提供了eclipse…/…运行时应用程序的路径。我正在制作eclipse插件,这个文件应该是我插件的一部分,所以当用户点击按钮时,这个文件就会打开(它是一些帮助txt文件)。这是我打开文件的代码,问题是路径

/**
 * Help button listener. If button is pressed, help file is opened.
 */
private void listenButtonHelp() {
    buttonHelp.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (Desktop.isDesktopSupported()) {
                File helpFile = new File("\\readme.txt");
                helpFile.setReadOnly();
                Desktop desktop = Desktop.getDesktop();
                try {
                    desktop.open(helpFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
}

这取决于文件在项目中的确切位置。放置它的干净点可能是${project.root}/resources,因此创建一个文件夹并将文件放在那里。在Eclipse中将其标记为“源文件夹”(项目属性->构建路径->源文件夹)。您当前的设置不是一个好主意,因为Eclipse的compile不会将该文件包含在您的发行版中

现在,当您编译代码时,它会被复制到目标控制器中(
bin
per default);您可以在文件浏览器中打开它进行检查

因此,要检查文件是否存在,您可以执行以下操作

Path filePath = Paths.get("resources", "readme.txt");
System.out.println(Files.exists(filePath));
File readmeFile = filePath.toFile();
如果您需要它作为
文件
,您可以这样做

Path filePath = Paths.get("resources", "readme.txt");
System.out.println(Files.exists(filePath));
File readmeFile = filePath.toFile();
这将从源项目文件夹中读取文件,因此在其他地方运行程序后不会有多大用处

为此,您可以使用
类加载器

URL readmeUrl = ClassLoader.getSystemClassLoader().getResource("resources/readme.txt"));
File readmeFile = new File(readmeUrl.getFile());

我找到了答案,这对我很有用:

/**
 * Help button listener. If button is pressed, help file is opened.
 */
private void listenButtonHelp() {
    buttonHelp.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            if (Desktop.isDesktopSupported()) {
                File file = null;
                Bundle bundle = Platform.getBundle("TestProject");
                IPath path = new Path("resources/readme.txt");
                URL url = FileLocator.find(bundle, path, null);
                /*
                 * After FileLocator, I get also this, like I commented before:
                 * D:\\eclipse-rcp-oxygen\\eclipse\\..\\..\\..\\eclipse_oxygen_workspace\\
                 * TestProject\\resources\\readme.txt and before it didn't work but if 
                 * you add these lines:
                 * url = FileLocator.toFileURL(url);
                 * file = URIUtil.toFile(URIUtil.toURI(url));

                 * Like in my try bracket, it works. I guess it needs to be 
                 * converted using URIUtil.
                 * Now it finds file, and it can be opened, also works for .html files.
                */
                Desktop desktop = Desktop.getDesktop();
                try {
                    url = FileLocator.toFileURL(url);
                    file = URIUtil.toFile(URIUtil.toURI(url));
                    // file.setReadOnly();
                    desktop.open(file);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    });
}

你试过使用(系统)属性吗?你是说这个System.getProperty(“user.dir”)?运行时没有“项目文件夹”的概念,如果你的项目中需要一个文件,你必须把它和编译结果放在一起。如何做到这一点取决于您的设置,例如您使用的是ant、Maven、Eclipse only等等。我只使用Eclipse,当您说编译结果时,您是指.class文件还是什么?使用classloader的getResource()方法。请参阅:我的readmeUrl为null,尝试了“resources/readme.txt”、“resources/readme.txt”、“resources\\readme.txt”、“resources\\readme.txt”,当我这样尝试时仍然是一样的:URL readmeUrl=this.getClass().getClassLoader().getresources(“resources/readme.txt”);输出为:bundleeresource://903.fwk984832924:1/resources/readme.txt 并且它不能作为文件打开。