不知道我写的这个是否是楼主想要的结果。
import java.io.File;public class test {
public static void main(String[] args) {
File file = new File("test.java");
String absolutPath = file.getAbsolutePath();
String dirPath = absolutPath.substring(0,absolutPath.lastIndexOf("\\"));
System.out.println(dirPath);
}
}
import java.io.File;public class test {
public static void main(String[] args) {
File file = new File("test.java");
String absolutPath = file.getAbsolutePath();
String dirPath = absolutPath.substring(0,absolutPath.lastIndexOf("\\"));
System.out.println(dirPath);
}
}
源文件.java在test\src\目录下
解释文件.class文件在test\bin\目录下.
程序运行的时候需要读取test\bin\目录下的一个现有的文件abc.txt
使用System.getProperty("user.dir");后,编译器提示在test\abc.txt文件不存在.
实际上我要定位到test\bin\abc.txt,所以所以还有各位大哥帮忙啊!