如何检测相对路径下的文件存在

首先这个问题已经google过了 不过我试了试不行。在csdn也请教了,还是没解决
献给大家简单介绍一下背景。
src为这个项目源码的目录,user为用户目录,src和user都在同一个项目文件夹PROJECT
下,也就是说二者有相同父路径PROJECT。如何检测user目录下有opml这个文件?以下是代码片段

 

File opml=new File("user\\a.txt");
if(opml.exists()==true){
    System.out.print("ok");
    }
else System.out.print("ok");  

 

这里是csdn的帖子http://topic.csdn.net/u/20090403/10/f7611d9f-02d6-4a56-a812-47056fef5781.html

 

希望大家集思广益 谢谢~


问题补充:
抱歉 错了代码是这样的
1.File opml=new File("user\a.txt");

2.if(opml.exists()==true){

3. System.out.print("ok");

4. }

5.else System.out.print("no");

问题补充:
photon的已经试过了。不显示错误 但是就是编译的时候不能通过。
如果代码如下的话,选择run as java bean就可以找到而且user.dir显示的是我工程的路径,如果编译工程就找不到,user.dir显示的C:\Documents and Settings\Administrator


File opml=new File(System.getProperty("user.dir")+"/user/a.txt");
public user(){
    if(opml.exists()){
        System.out.print("ok");
        //System.out.println(System.getProperty("user.dir"));   
    }
    else {System.out.print("no");
    //System.out.println(System.getProperty("user.dir")); 
    }
    System.out.println(System.getProperty("user.dir")); 
}</pre><br /><strong>问题补充:</strong><br />我再补充一下吧。。。这不是个web项目。仅仅是个离线阅读器的项目而已<br /><strong>问题补充:</strong><br /><span style="color: red"><span style="font-size: xx-large">我再补充一下吧。。。这不是个web项目。仅仅是个离线阅读器的项目而已 </span></span>

试试这个
[code="java"]
String thePath=ClassLoader.getSystemResource("").getPath();

    File opml=new File(thePath + "/" +"user\\a.txt");   
    if(opml.exists()==true){   
        System.out.print("ok");   
        }   
    else System.out.print("ok");

[/code]

参考:http://www.iteye.com/topic/117978

LZ,不管这文件存在与否,你都打了OK……
另外,可以试下把路径用/而不是\。
[code="java"]if(opml.exists()) {
//...
}[/code]
就可以了。

楼助试过我的代码了吗,效果如何?

我不知道你使用了是应用服务器,比如tomcat,weblogic,websphere等。

在我前边回复给出的参考链接里,有这么一段话
[quote]3.2 Servlet中获得当前应用的相对路径和绝对路径
根目录所对应的绝对路径:request.getServletPath();
文件的绝对路径 :request.getSession().getServletContext().getRealPath
(request.getRequestURI())
当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");
(ServletContext对象获得几种方式:
javax.servlet.http.HttpSession.getServletContext()
javax.servlet.jsp.PageContext.getServletContext()
javax.servlet.ServletConfig.getServletContext()
)
[/quote]
我没有试验这段代码,楼主可以试着参照已有的代码自己试验一下,或者看看文中提供的其它建议是否可行。

郁闷,这么关键的没说……
[code="java"]getClass().getResourceAsStream("/mvc.txt")[/code]
注意,这里的路径参照点是WEB-INF下的classes文件夹,这个mvc.txt文件完整路径是WEB-INF/classes/mvc.txt
以上代码在两年前的一个项目里用过,当时是Tomcat服务器,不知道现在的是否会有变。

[quote]选择run as java bean[/quote]
仅仅是好奇,你这个bean不需要web容器吗?