java JAVA项目中如何获取文件绝对路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31466448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:39:54  来源:igfitidea点击:

How to get the file absolute path in JAVA project

java

提问by user819774

Hi I need to read the file by using FileInputStream but I didn't get the right path. My file is location at C:\Users\tester\Documents\Java Project\Samples\ProjectOne\src\pdfReader However when I used the below code, I get the wrong path that is "/C:/Users/tester/Documents/Java%20Project/Samples/ProjectOne/bin/ProjectOne/TestFile.txt"

嗨,我需要使用 FileInputStream 读取文件,但我没有找到正确的路径。我的文件位于 C:\Users\tester\Documents\Java Project\Samples\ProjectOne\src\pdfReader 但是当我使用下面的代码时,我得到了错误的路径“/C:/Users/tester/Documents/ Java%20Project/Samples/ProjectOne/bin/ProjectOne/TestFile.txt"

There is my code:

有我的代码:

String filePath;
filePath=MainForm.class.getResource("TestFile.txt").getPath();

Would someone tell me how to get the file path?

有人会告诉我如何获取文件路径吗?

回答by Fernando Costa

You are probably using Eclipse and as you have saved the file TestFile.txtinside your source folder it is being copied to the binfolder, the output folder of your project. Therefore, the path is not wrong. As in your code you use getResourcemethod, the file will be retrieved from the same directory where your MainForm.classwas found.

您可能正在使用 Eclipse,并且当您将文件保存在TestFile.txt源文件夹中时,它被复制到bin文件夹,即项目的输出文件夹。因此,路径没有错。与在您使用getResource方法的代码中一样,将从您MainForm.class找到的同一目录中检索该文件。

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)

http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)

If you really want to get such file from your source folder, then you should do something like this:

如果您真的想从源文件夹中获取此类文件,则应该执行以下操作:

System.out.println(new File("src/pdfReader/TestFile.txt").getAbsolutePath());

However, if you are planning to distribute your application it would be better to store this kind of file in a resourcesfolder because source folders are usually not included in distpackages.

但是,如果您计划分发应用程序,最好将此类文件存储在resources文件夹中,因为源文件夹通常不包含在dist包中。

回答by Anand Dwivedi

You can create fileand use getAbsolutePathmethod:

您可以创建file和使用getAbsolutePath方法:

File file = new File("TestFile.txt");//full file path URL
String absolutePath = file.getAbsolutePath();

Here is the simple programme:

这是一个简单的程序:

public static void main(String[] args) {
    File f = null;
    String path = "";
    boolean bool = false;
    try {
        // create new files
        f = new File("test.txt");
        // returns true if the file exists
        bool = f.exists();
        // if file exists
        if (bool) {
            // get absolute path
            path = f.getAbsolutePath();
            // prints
            System.out.print("Absolute Pathname " + path);
        }
    } catch (Exception e) {
        // if any error occurs
        e.printStackTrace();
    }
}