示例示例在java中获取本地文件路径有两种方式:使用System)方法:
在java中获取本地文件路径有两种方式:
1.使用System.getProperty()方法:
System.getProperty("user.dir")可以获取当前项目的根路径:
String path = System.getProperty("user.dir");
2.使用File类:
File file = new File("文件名");
String path = file.getAbsolutePath();
以上两种方式都可以获取本地文件的路径。
是一个示例代码:
public class GetLocalFilePath {
public static void main(String[] args) {
// 使用System.getProperty()方法获取本地文件路径
String path1 = System.getProperty("user.dir");
System.out.println("使用System.getProperty()方法获取本地文件路径:" + path1);
// 使用File类获取本地文件路径
File file = new File("test.txt");
String path2 = file.getAbsolutePath();
System.out.println("使用File类获取本地文件路径:" + path2);
}
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(72条)