问题:
selectBean.class和temp_zh.properties文件同在一个目录下。
在selectBean中访问temp_zh.properties文件
try{
FileInputStream fis=new FileInputStream("temp_zh.properties");
}catch(FileNotFoundException e){
e.printStackTrace();
}程序报错:
java.io.FileNotFoundException: temp_zh.properties (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at com.cyf.struts.SelectBean.main(SelectBean.java:27)以为是系统环境变量classpath的问题
这是我的设置:classpath=.;E:\JDK\lib\Tools.jar;E:\JDK\lib\rt.jar;
selectBean.class和temp_zh.properties文件同在一个目录下。
在selectBean中访问temp_zh.properties文件
try{
FileInputStream fis=new FileInputStream("temp_zh.properties");
}catch(FileNotFoundException e){
e.printStackTrace();
}程序报错:
java.io.FileNotFoundException: temp_zh.properties (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at com.cyf.struts.SelectBean.main(SelectBean.java:27)以为是系统环境变量classpath的问题
这是我的设置:classpath=.;E:\JDK\lib\Tools.jar;E:\JDK\lib\rt.jar;
那有什么办法得到temp_zh.properties的绝对路径呢?
FileInputStream类找的是文件系统,所以说你必须给出一个文件的绝对路径。而如果需要读取当前class下的文件的话,需要用Class.getResourceAsStream(String name)方法
相对路径也是可以的,不过要相对于你的工程根目录