您的位置:首页 > 编程语言 > Java开发

Java文件获取路径方式

2015-03-05 10:35 190 查看
由于经常需要获取文件的路径,但是比较容易忘记,每次需要总需要查询,现在把这些方式写下来,方便自己的时候也方便大家了,如果大家在下面的方法遇到什么问题,可以留言。

Java文件获取路径方式:

package first.second;
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(""));
//方式4
System.out.println(GetPath.class.getClassLoader().getResource(""));
System.out.println(GetPath.class.getClassLoader().getResource("source.xml"));
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GetPath.getPath();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息