转载)相对路径读文件

来源:互联网 发布:黑色星期五禁曲 知乎 编辑:程序博客网 时间:2024/04/30 11:57
不管你是新手还是老鸟,在程序中读取资源文件总会遇到一些找不到文件的问题,这与Java底层的实现有关,不能算bug,只要方法得当,问题还是可以解决的。 

项目的文件夹结构: 
repathtest 
├─src 
│    └─com 
│            └─lavasoft 
│                    ├─test 
│                    └─res 
├─doc 




1、在Java开发工具的project中使用相对路径 
在project中,相对路径的根目录是project的根文件夹,在此就是repathtest文件夹了。 
创建文件的写法是: 
File f = new File("src/com/lavasoft/res/a.txt"); 

File f = new File("doc/b.txt"); 

注意: 
路径不以“/”开头; 
脱离了IDE环境,这个写法就是错误的,也并非每个IDE都如此,但我见到的都是这样的。 

2、通过CLASSPATH读取包内文件 
读取包内文件,使用的路径一定是相对的classpath路径,比如a,位于包内,此时可以创建读取a的字节流: 
InputStream in = ReadFile.class.getResourceAsStream("/com/lavasoft/res/a.txt"); 
有了字节流,就能读取到文件内容了。 

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

3、看看完整的测试代码
[java] view plaincopy
  1. package com.lavasoft.test;  
  2. import java.io.*;  
  3. /** 
  4. * Java读取相对路径的文件 
  5. * 
  6. * @author leizhimin 2010-1-15 10:59:43 
  7. */  
  8. public class ReadFile {  
  9.         public static void main(String[] args) {  
  10.                 readTextA_ByClassPath();  
  11.                 readTextA_ByProjectRelativePath();  
  12.                 readTextB_ByProjectRelativePath();  
  13.         }  
  14.         /** 
  15.          * 通过工程相对路径读取(包内)文件,注意不以“/”开头 
  16.          */  
  17.         public static void readTextA_ByProjectRelativePath() {  
  18.                 System.out.println("-----------------readTextA_ByProjectRelativePath---------------------");  
  19.                 File f = new File("src/com/lavasoft/res/a.txt");  
  20.                 String a = file2String(f, "GBK");  
  21.                 System.out.println(a);  
  22.         }  
  23.         /** 
  24.          * 通过工程相对路径读取(包外)文件,注意不以“/”开头 
  25.          */  
  26.         public static void readTextB_ByProjectRelativePath() {  
  27.                 System.out.println("-----------------readTextB_ByProjectRelativePath---------------------");  
  28.                 File f = new File("doc/b.txt");  
  29.                 String b = file2String(f, "GBK");  
  30.                 System.out.println(b);  
  31.         }  
  32.         /** 
  33.          * 通过CLASSPATH读取包内文件,注意以“/”开头 
  34.          */  
  35.         public static void readTextA_ByClassPath() {  
  36.                 System.out.println("-----------------readTextA_ByClassPath---------------------");  
  37.                 InputStream in = ReadFile.class.getResourceAsStream("/com/lavasoft/res/a.txt");  
  38.                 String a = stream2String(in, "GBK");  
  39.                 System.out.println(a);  
  40.         }  
  41.         /** 
  42.          * 文件转换为字符串 
  43.          * 
  44.          * @param f             文件 
  45.          * @param charset 文件的字符集 
  46.          * @return 文件内容 
  47.          */  
  48.         public static String file2String(File f, String charset) {  
  49.                 String result = null;  
  50.                 try {  
  51.                         result = stream2String(new FileInputStream(f), charset);  
  52.                 } catch (FileNotFoundException e) {  
  53.                         e.printStackTrace();  
  54.                 }  
  55.                 return result;  
  56.         }  
  57.         /** 
  58.          * 文件转换为字符串 
  59.          * 
  60.          * @param in            字节流 
  61.          * @param charset 文件的字符集 
  62.          * @return 文件内容 
  63.          */  
  64.         public static String stream2String(InputStream in, String charset) {  
  65.                 StringBuffer sb = new StringBuffer();  
  66.                 try {  
  67.                         Reader r = new InputStreamReader(in, charset);  
  68.                         int length = 0;  
  69.                         for (char[] c = new char[1024]; (length = r.read(c)) != -1;) {  
  70.                                 sb.append(c, 0, length);  
  71.                         }  
  72.                         r.close();  
  73.                 } catch (UnsupportedEncodingException e) {  
  74.                         e.printStackTrace();  
  75.                 } catch (FileNotFoundException e) {  
  76.                         e.printStackTrace();  
  77.                 } catch (IOException e) {  
  78.                         e.printStackTrace();  
  79.                 }  
  80.                 return sb.toString();  
  81.         }  
  82. }   


(代码写得粗糙,异常没做认真处理) 

运行结果: 
-----------------readTextA_ByClassPath--------------------- 
aaaaaaaaa 
sssssssss 
-----------------readTextA_ByProjectRelativePath--------------------- 
aaaaaaaaa 
sssssssss 
-----------------readTextB_ByProjectRelativePath--------------------- 
bbbbbbbbbbb 

Process finished with exit code 0
0 0
原创粉丝点击