简体   繁体   English

如何获取当前文件的完整路径,包括java中的src文件

[英]how to get full path of the current file including the src file in java

I have a file dateTesting.java . 我有一个文件dateTesting.java。 the path's directory is as follows: D:\\workspace\\Project1\\src\\dateTesting.java . 路径的目录如下:D:\\ workspace \\ Project1 \\ src \\ dateTesting.java。 I want the full path of this file as "D:\\workspace\\Project1\\src" itself but when I use any of the following code, i get only "D:\\workspace\\Project1" . 我希望此文件的完整路径本身为“ D:\\ workspace \\ Project1 \\ src”,但是当我使用以下任何代码时,我只会得到“ D:\\ workspace \\ Project1”。 the src part is not coming. src部分不来了。

System.out.println(System.getProperty("user.dir"));
File dir2 = new File(".");
System.out.println(dir2.getCanonicalPath().toString());
System.out.println(dir2.getAbsolutePath());

How can I get the full path as "D:\\workspace\\Project1\\src" ? 如何获得完整路径为“ D:\\ workspace \\ Project1 \\ src”? I'm using eclipse ide 3.5 我正在使用Eclipse IDE 3.5

Thank you 谢谢

dateTesting.java is a Java source file which is not available after compilation to bytecode. dateTesting.java是Java源文件,编译为字节码后不可用。 The source directory it was in is not available, too. 它所在的源目录也不可用。

dir2 is the File of the directory you execute the .class file in. It seams that this happens to be D:\\workspace\\Project1 but you can't rely on this. dir2是在其中执行.class文件的目录的File 。它似乎是D:\\workspace\\Project1但您不能依靠它。

Your dir2 points to working directory ( new File(".") ). 您的dir2指向工作目录( new File(".") )。 You can't get the location of your sources this way. 您无法通过这种方式获取源的位置。 Your file could sit inside the package (eg your.company.date.dateTesting ). 您的文件可以放在包中(例如your.company.date.dateTesting )。 You should just manually concat the "src" to current working directory and then replace file package dots ( . ) with File.pathSeparator . 您应该只手动将“ src”连接到当前工作目录,然后用File.pathSeparator替换文件包点( . )。 In that way you will build the full path to your file. 这样,您将构建文件的完整路径。

String fullFilePath = "H:\\Shared\\Testing\\abcd.bmp";
    File file = new File(fullFilePath);

    String filePath = file.getAbsolutePath().substring(0,fullFilePath.lastIndexOf(File.separator));
    System.out.println(filePath);

Output: H:\\Shared\\Testing 输出:H:\\ Shared \\ Testing

If you are doing this to try to read a file from the classpath, then check out this answer: How to really read text file from classpath in Java 如果您这样做是为了尝试从类路径中读取文件,请查看以下答案: 如何从Java中的类路径中真正读取文本文件

Essentially you can do this 基本上你可以做到

InputStream in = this.getClass().getClassLoader()
                            .getResourceAsStream("SomeTextFile.txt");

Otherwise, if you have some other requirement, one option is to pass through the src directory as a JVM arg when the application begins and then just read it back. 否则,如果您有其他要求,一种选择是在应用程序启动时以JVM arg的身份通过src目录,然后将其读回。

/** The actual file running */
public static final File    JAR_FILE = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();

/** The path to the main folder where the file .jar is run */
public static final String  BASE_DIRECTORY  = (JAR_FILE != null ? JAR_FILE.getAbsolutePath().replace(JAR_FILE.getName(), "") : "notFound");

This will work for you both in normal java execution and jar execution. 这将在正常的Java执行和jar执行中为您工作。 This is the solution I am using in my project. 这是我在项目中使用的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM