JAVA获取文件路径

来源:互联网 发布:如何做好淘宝 蒋晖 编辑:程序博客网 时间:2024/06/08 06:22
public static void main(String args[])  
    {  
        //方式一  
        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(""));  //获取编译后的项目根目录,类似(GetPath.class.getResource("/"))
        System.out.println(GetPath.class.getClassLoader().getResource("source.xml"));  //获取编译后的source.xml文件目录(若文件不存在则返回null)默认以项目根目录寻找

    }  

----------------------------------------------------------------------------------------------------------------------------------------------------------------

方式一  
D:\eclipse\juno_workspace\Test  
方式二  
D:\eclipse\juno_workspace\Test  
D:\eclipse\juno_workspace\Test  
方式三  
file:/D:/eclipse/juno_workspace/Test/bin/  
file:/D:/eclipse/juno_workspace/Test/bin/first/second/  
方式四  
file:/D:/eclipse/juno_workspace/Test/bin/  (file:/D:/eclipse/juno_workspace/Test/WebRoot/WEB-INF/classes/)
file:/D:/eclipse/juno_workspace/Test/bin/source.xml (file:/D:/eclipse/juno_workspace/Test/WebRoot/WEB-INF/classes/source.xml)

----------------------------------------------------------------------------------------------------------------------------------------------------------------

//获取WebRoot目录  
    public static String getWebRootPath()  
    {  
        URL urlpath=GetPath.class.getResource("");  
        String path=urlpath.toString();  
        if(path.startsWith("file"))  
        {  
            path=path.substring(5);  
        }  
        if(path.indexOf("WEB-INF")>0)  
        {  
            path=path.substring(0,path.indexOf("WEB-INF")-1);  
        }  
        path.replace("/", File.separator);  
        return path;  
    }  
    //webroot  WebRoot目录  
    //filename  文件名  
    //...args   文件名所在文件夹,多个参数输入  
    public static String getWebRootFilepath(String webroot,String filename,String ...args)  
    {  
        String pre=webroot;  
        String path=pre;  
        for(String arg:args)  
        {  
            path+=File.separator+arg;  
        }  
        path+=File.separator+filename;  
        if(path.startsWith("file"))  
        {  
            path=path.substring(5);  
        }  
        path.replace("/", File.separator);  
        return path;  
    }  

-----------------------------------------------------------------------------------------------------------------------------------------------------

注意一个问题:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("spring-application.xml");

有些人喜欢使用此方法去加载一个文件,但是这个方法默认是加载编译后的文件的根目录路径,(这里就算指定绝对路径都无效)

如果你的文件不是放在根目录的话,还是按照上面方法去加载相关文件,不然会出现找不到文件的异常。


转自http://blog.csdn.net/appleprince88/article/details/11599805




0 0