在文件操作中,最讨厌,最烦人的就是绝对路径和相对路径的问题。

经常出现NullPointerException,File not exists等等。

在51CTO博客上找到一篇不错的Java路径的介绍。在这里记录一下。

 

原文地址:http://lavasoft.blog.51cto.com/62575/265821

 



一般常见的项目文件夹结构:

repathtest 
   
├─src 
   
│    └─com 
   
│            └─lavasoft 
   
│                    ├─test 
   
│                    └─res 
   
├─doc


1、在Java开发工具的project中使用相对路径



在project中,相对路径的根目录是project的根文件夹,在此就是repathtest文件夹了。



创建文件的写法是:


new File( 
    "src/com/lavasoft/res/a.txt"); 
   
 
  
new File( 
    "doc/b.txt");


注意:



路径不以“/”开头;



脱离了IDE环境,这个写法就是错误的,也并非每个IDE都如此,但我见到的都是这样的。



 



2、通过CLASSPATH读取包内文件



读取包内文件,使用的路径一定是相对的classpath路径,比如a,位于包内,此时可以创建读取a的字节流:


class.getResourceAsStream( 
    "/com/lavasoft/res/a.txt");



有了字节流,就能读取到文件内容了。



 注意:这里必须以“/”开头;



3、看看完整的测试代码



package com.lavasoft.test; 

import java.io.*; 

/** 
* Java读取相对路径的文件 
* 
* @author leizhimin 2010-1-15 10:59:43 
*/ 
public class ReadFile { 
        public static void main(String[] args) { 
                readTextA_ByClassPath(); 
                readTextA_ByProjectRelativePath(); 
                readTextB_ByProjectRelativePath(); 
        } 

        /** 
         * 通过工程相对路径读取(包内)文件,注意不以“/”开头 
         */ 
        public static void readTextA_ByProjectRelativePath() { 
                System.out.println("-----------------readTextA_ByProjectRelativePath---------------------"); 
                File f = new File("src/com/lavasoft/res/a.txt"); 
                String a = file2String(f, "GBK"); 
                System.out.println(a); 
        } 

        /** 
         * 通过工程相对路径读取(包外)文件,注意不以“/”开头 
         */ 
        public static void readTextB_ByProjectRelativePath() { 
                System.out.println("-----------------readTextB_ByProjectRelativePath---------------------"); 
                File f = new File("doc/b.txt"); 
                String b = file2String(f, "GBK"); 
                System.out.println(b); 
        } 


        /** 
         * 通过CLASSPATH读取包内文件,注意以“/”开头 
         */ 
        public static void readTextA_ByClassPath() { 
                System.out.println("-----------------readTextA_ByClassPath---------------------"); 
                InputStream in = ReadFile.class.getResourceAsStream("/com/lavasoft/res/a.txt"); 
                String a = stream2String(in, "GBK"); 
                System.out.println(a); 
        } 

        /** 
         * 文件转换为字符串 
         * 
         * @param f             文件 
         * @param charset 文件的字符集 
         * @return 文件内容 
         */ 
        public static String file2String(File f, String charset) { 
                String result = null; 
                try { 
                        result = stream2String(new FileInputStream(f), charset); 
                } catch (FileNotFoundException e) { 
                        e.printStackTrace(); 
                } 
                return result; 
        } 

        /** 
         * 文件转换为字符串 
         * 
         * @param in            字节流 
         * @param charset 文件的字符集 
         * @return 文件内容 
         */ 
        public static String stream2String(InputStream in, String charset) { 
                StringBuffer sb = new StringBuffer(); 
                try { 
                        Reader r = new InputStreamReader(in, charset); 
                        int length = 0; 
                        for (char[] c = new char[1024]; (length = r.read(c)) != -1;) { 
                                sb.append(c, 0, length); 
                        } 
                        r.close(); 
                } catch (UnsupportedEncodingException e) { 
                        e.printStackTrace(); 
                } catch (FileNotFoundException e) { 
                        e.printStackTrace(); 
                } catch (IOException e) { 
                        e.printStackTrace(); 
                } 
                return sb.toString(); 
        } 
}

 


5、获取CLASSPATH下文件的绝对路径



当使用相对路径写入文件时候,就需要用到绝对路径。下面是个例子:



package com.lavasoft; 

import java.io.File; 

/** 
* CLASSPATH文件的绝对路径获取测试 
* 
* @author leizhimin 2010-1-18 9:33:02 
*/ 
public class Test { 
        //classpath的文件路径 
        private static String cp = "/com/lavasoft/cfg/syscfg.properties"; 

        public static void main(String[] args) { 
                //当前类的绝对路径 
                System.out.println(Test.class.getResource("/").getFile()); 
                //指定CLASSPATH文件的绝对路径 
                System.out.println(Test.class.getResource(cp).getFile()); 
                //指定CLASSPATH文件的绝对路径 
                File f = new File(Test.class.getResource(cp).getFile()); 
                System.out.println(f.getPath()); 
        } 
}

 


总结



使用工程相对路径是靠不住的。



使用CLASSPATH路径是可靠的。



对于程序要读取的文件,尽可能放到CLASSPATH下,这样就能保证在开发和发布时候均正常读取



 



本文出自 “熔 岩” 博客,请务必保留此出处http://lavasoft.blog.51cto.com/62575/265821