简体   繁体   English

如何处理此FileNotFoundException?

[英]How do I handle this FileNotFoundException?

    File file=new File("login.properties");
    FileInputStream input=null;
    try{
         input=new FileInputStream(file);// line33
    }
    catch(FileNotFoundException e )
    {
        e.printStackTrace();
    }
    Properties prop =new Properties();
    prop.load(input);
    input.close();

I'm getting FileNotFoundException at line33. 我在第33行收到FileNotFoundException。 i have "login.properties" file in WebContent folder. 我在WebContent文件夹中有“ login.properties”文件。 Where do I exactly place that file? 我应该将该文件放在哪里?

I will take it as you are trying to make a webapp. 在您尝试制作Webapp时,我会考虑一下。

You can access a path using request.getRealPath(request.getServletPath()); 您可以使用request.getRealPath(request.getServletPath());访问路径request.getRealPath(request.getServletPath()); after that you can add the rest of the path of your file. 之后,您可以添加文件的其余路径。

For example in weblogic you will get your path till your domain c:\\oracle\\middleware\\oracle_home\\user_projects\\domains\\YourDomain\\ returned by above code. 例如,在weblogic中,您将获得直到上述代码返回的域c:\\oracle\\middleware\\oracle_home\\user_projects\\domains\\YourDomain\\的路径。

your file might reside in applications\\YourApplication\\WEB-INF\\login.properties inside YourDomain folder. 您的文件可能位于YourDomain文件夹内的applications\\YourApplication\\WEB-INF\\login.properties So use above code to get path till your domain, then add rest of the path yourself. 因此,使用上面的代码获取到达您的域的路径,然后自己添加路径的其余部分。

The best way is to put the path in web.xml using context-param 最好的方法是使用context-param将路径放在web.xml

<context-param>
<param-name>loginPropFileLoc</param-name>
<param-value>c:\oracle\middleware\oracle_home\user_projects\domains\YourDomain\YourApplication\WEB-INF\login.properties</param-value>
</context-param>

Then access the login properties file.Through this value. 然后访问登录属性文件。通过此值。

ELSE put the properties file in the same directory where your class file is present. ELSE将属性文件放在类文件所在的目录中。 If your class file is in \\abcd\\ReadProperty.class then put property file as \\abcd\\login.properties 如果您的类文件位于\\abcd\\ReadProperty.class则将属性文件作为\\abcd\\login.properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM