日期:2014-05-18  浏览次数:20831 次

大家帮帮忙看看,为什么读取的文件路径是错误的?
小弟写的关于java读取配置文件的方法,一种是采用类装载器,一种是下面的方法,但是类装载器,读取的文件内容没有问题,不知道为什么下面的这中方式读取的问价路径不正确,希望大侠帮忙看看!



public void delete(){
try {
String path = UserDao.class.getClassLoader().getResource("db.properties").getPath();  
System.out.println("文件的路径为:"+path);
 FileInputStream in = new FileInputStream(path);  
Properties prop=new Properties();

prop.load(in);

    
    String url=prop.getProperty("url");
    String username=prop.getProperty("username");
    String passwd=prop.getProperty("passwd");
    System.out.println("------这是通过传统方式获取的配置文件----------");
    System.out.println("url==:"+url+"  用户名="+username+"   密码="+passwd);
} catch (Exception e) {
e.printStackTrace();
}
    
}

这是异常代码:

文件的路径为:/D:/Program%20Files/Apache%20Software%20Foundation/Tomcat%205.5/webapps/Servlet/WEB-INF/classes/db.properties
java.io.FileNotFoundException: D:\Program%20Files\Apache%20Software%20Foundation\Tomcat%205.5\webapps\Servlet\WEB-INF\classes\db.properties (系统找不到指定的路径。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at com.java.test.UserDao.delete(UserDao.java:40)
at com.servlet.test.ServletContextCommuncation.doGet(ServletContextCommuncation.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:627)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:879)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:722)



问个JBOSS小疑点