首先我们要知道,如果在Eclipse项目中直接用类似 new FileReader("1.txt") 这样不带路径直接用文件名访问文件的时候,这个文件必须是在项目的根目录下。也就是说,Eclipse中Java程序的当前工作目录都是在项目根目录(与src平级)下的。
二、bin
但是,我们会发现,很多时候用 new FileReader("1.txt") 的方式不能访问到我们需要的文件,必须要用 new FileReader("bin/1.txt")来访问。
这个很好解释,因为java程序需要先编译成class的字节码文件然后再再运行,而程序运行的时候class文件正处于项目根目录下的 bin 目录中,所以其工作目录就是 bin 目录了。不过一般我们编译的时候,Eclipse会自动帮我们把项目依赖的文件复制一份,放到 bin 目录下的。
三、中文!
用Eclipse写了一个简单的程序,发现我们无法通过 new FileReader("1.txt") 或者是 new FileReader("bin/1.txt") 来访问文件。之后我通过在程序中输出 Class.class.getClass().getResource("/").getPath()的值发现,输出的是:/D:/workspace/xxxx/bin/ 是项目路径中带有中文惹的祸吧。
转自:http://blog.csdn.net/jinzheng069/article/details/21739365
---------------------------------------------------------我是万恶的分割线-----------------------------------------------
项目的文件夹结构:
├─src
│ └─com
│ └─lavasoft
│ ├─test
│ └─res
│ └─a.txt
├─doc
│ └─b.txt
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();
}
/**
* 通过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);
}
/**
* 通过工程相对路径读取(包内)文件,注意不以“/”开头
*/
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);
}
/**
* 文件转换为字符串
*
* @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();
}
}
aaaaaaaaa
sssssssss
-----------------readTextA_ByProjectRelativePath---------------------
aaaaaaaaa
sssssssss
-----------------readTextB_ByProjectRelativePath---------------------
bbbbbbbbbbb
Process finished with exit code 0

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());
}
}
/D:/projects/bbt/code/cdn/planrpt/out/production/planrpt/com/lavasoft/cfg/syscfg.properties
D:\projects\bbt\code\cdn\planrpt\out\production\planrpt\com\lavasoft\cfg\syscfg.properties
Process finished with exit code 0