public static void main(String[] args) throws Exception
{
IniReader aa =new IniReader("e:\\mail.properties");
}
{
IniReader aa =new IniReader("e:\\mail.properties");
}
解决方案 »
- 怎样同步对象的某个属性的访问(Getter和Setter只能有一个线程访问访问其中一个)
- 关于读取文件权限奇怪问题?????????????
- 关于EXCEL求助
- 关于梦幻西游走路问题
- sqlserver 2000 打sp3或sp4 提示:move data process:-145
- 有谁知道JCREATOR里为什么在编译applet文件时,能编译通过,可一运行就提示:没有main函数呀!
- 为什么inputMap不能直接地将KeyStroke映射到Action对象
- 请问各位大虾:Java中的Servlet、JavaBean、Rmi还有tomcat和apach分别是意思,它们是干什么的?来者有分!!!!
- 请问有关classLoader类的问题?????
- Java程序中如何执行系统命令?
- 关于classLoader getResourceAsStream的一个问题 急需解决
- 请求getResourceAsStream()方法的使用????
@see Class#getResourceAsStream(String)
..
看看文档的描述,它并不是从绝对路径去搜索你的文件啊。。
Finds a resource with a given name.
这个功能是由class loader来实现的,会委托到加载这个对象的class loader去实现。
在委托之前,绝对资源名字会按照以下的算法从你给定的资源名称构造出来:
(1) If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
(2) Otherwise, the absolute name is of the following form:
modified_package_name/name
所以,按照你的做法,实际去找的文件是classpath/e:/mail.properties,这个当然找不到了。针对你的问题,有两种修改的方法:
(1) 既然你已经知道绝对路径了,这样做就可以了,没有必要用getResourceAsStream:
<<
in = new FileInputStream(new File(strFileName));
>>(2) 把mail.properties放到classpath中
<<
IniReader aa = new IniReader("mail.properties"); 在IniReader中:
in = IniReader.class.getResourceAsStream("/"+strFileName);
>>
第二种做法会稍微合理一些。。
c:\IniReader.class
c:\prop\mail.properties则
可以这么写:
IniReader aa =new IniReader("prop//mail.properties");
程序:
public class HelloWorld {
public static void main(String[] args) throws Exception {
java.io.InputStream is = HelloWorld.class.getResourceAsStream("/mail.properties");
System.out.println(is);
}
}
运行:
$ java HelloWorld
null
假设把mail.properties放到conf目录下
$ java -classpath .:conf HelloWorld
java.io.BufferedInputStream@11a698a这样就可以了
我用的是系统是2000,我直接在环境变量里设置也没用,用set classpath=...也没用。