关于Eclipse的项目下路径获取问题

Machars 2014-10-17 01:19:46
问题是这样的:我在项目的src路径下有有个CityMail.properties文件
如果我是平常的类中读取改文件,应该是这样:FileInputStream fis = new FileInputStream("src/CityMail.properties");
经过我实践,发现:假如我的类中有main函数入口,直接run/java application运行该文件,则没有任何问题
MailAction 类文件
public class MailAction extends TimerTask {
static {
try{
FileInputStream fis = new FileInputStream("src/CityMail.properties");
fis.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
}
}
MyListener 类文件
public class MyListener implements ServletContextListener {
Timer timer = new Timer();
TimerTask task = new MailAction();
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
timer.schedule(task, 0,600000);
}
}
如果我运行的是web程序,在web.xml中定义了一个定时器截获MyListener ,再由MyListener 调用MailAction ,这里就会抛出:系统找不到指定的路径。
这是什么回事?我该如何得到该路径?虔心求指教!!
...全文
237 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Machars 2014-10-17
  • 打赏
  • 举报
回复
引用 1 楼 slwsss 的回复:
看下这个 http://lengbingteng-163-com.iteye.com/blog/1115568
经过测试发觉:有web.xml的listener配置调用的所有的类或者函数,他显示的路径都是在/WebContent/WEB-INF下的,比如我想要获取的src目录的CityMail.properties文件,它的路径却是file:/C:/apache-tomcat-7.0.55/webapps/富顺天牧/WEB-INF/classes/CityMail.properties 虽然说CityMail.properties 确实是在src下,但 可以看出运行后的CityMail.properties 路径与src毫无关系,这就是为什么老是提示:“找不到系统路径”的原因了!
Machars 2014-10-17
  • 打赏
  • 举报
回复
引用 1 楼 slwsss 的回复:
看下这个 http://lengbingteng-163-com.iteye.com/blog/1115568
虽然很简单易懂,但它需要引入Eclipse的包,有没有不需要引入其他包的方法?
slwsss 2014-10-17
  • 打赏
  • 举报
回复
看下这个 http://lengbingteng-163-com.iteye.com/blog/1115568

10,606

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧