- package com.zjf;
- import java.io.File;
- public class GetPath {
- public static void getPath()
- {
- //方式一
- System.out.println(System.getProperty("user.dir"));
- //方式二
- File directory = new File("");//设定为当前文件夹
- try{
- System.out.println(directory.getCanonicalPath());//获取标准的路径
- System.out.println(directory.getAbsolutePath());//获取绝对路径
- }catch(Exception e)
- {
- e.printStackTrace();
- }
- //方式三
- System.out.println(GetPath.class.getResource("/"));
- System.out.println(GetPath.class.getResource(""));
- //方式四
- System.out.println(GetPath.class.getClassLoader().getResource(""));
- System.out.println(GetPath.class.getClassLoader().getResource("source.xml"));
- }
- public static void main(String[] args) {
- GetPath.getPath();
- }
35. }