题目要求:
用java实现: pwd:打印当前目录
方法一:
利用 System.getProperty(“user.dir”);方法获取当前路径
package com.company;
import java.io.File;
public class Main {
public static void main(String[] args) {
String str=System.getProperty("user.dir");
System.out.println(str);
}
}
方法二:
先新建File类
在利用File获取路径
System.out.println(file.getAbsolutePath());//获取绝对路径
System.out.println(file.getCanonicalPath());//获取标准路径
package com.company;
import java.io.File;
public class Main {
public static void main(String[] args) {
File file=new File("");//新建File类
try {
System.out.println(file.getAbsolutePath());//获取绝对路径
System.out.println(file.getCanonicalPath());//获取标准路径
}
catch (Exception e) {
System.out.println("error");
}//异常处理
}
}
方法三:
通过Thread类实现
System.out.println(Thread.currentThread().getContextClassLoader().getResource("./").getPath());
System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());//ClassPath的绝对URI路径
System.out.println(Thread.currentThread().getContextClassLoader().getResource(".").getPath());//项目的绝对路径
ackage com.company;
import java.io.File;
public class Main {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getContextClassLoader().getResource("./").getPath());
System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath());//ClassPath的绝对URI路径
System.out.println(Thread.currentThread().getContextClassLoader().getResource(".").getPath());//项目的绝对路径
}
}