Java JSP-系统找不到指定的文件

Java JSP-系统找不到指定的文件,java,jsp,Java,Jsp,我在java中创建了一个函数,用于验证文件属性是否存在,如果存在,它将检索其内容,否则它将通过在内部写入来创建它。 通过eclipse从java应用程序启动此函数,它会正确运行,而从jsp页面调用此函数时,不会找到文件并给出错误 这是我的java函数: private static File fileName = new File("properties.properties"); Properties props = new Properties(); public void writePr

我在java中创建了一个函数,用于验证文件属性是否存在,如果存在,它将检索其内容,否则它将通过在内部写入来创建它。 通过eclipse从java应用程序启动此函数,它会正确运行,而从jsp页面调用此函数时,不会找到文件并给出错误

这是我的java函数:

private static File fileName = new File("properties.properties");
Properties props = new Properties();

public void writeProperties() throws FileNotFoundException, IOException{        
    if(fileName.exists()){
        props.load(new FileInputStream(fileName));
    }

    boolean equal = false;
    if(!UtilsFunction.isEmpty(props.getProperty("table-list"))){
        String [] arr = props.getProperty("table-list").split(",");

        for(int i=0;i<arr.length;i++){
            if(arr[i].trim().equals(tableName)){
                equal = true;
                break;
            }
        }
        if(!equal){
            props.setProperty("table-list", props.getProperty("table-list")+","+tableName);
        }
    }else{
        props.setProperty("table-list", tableName);
    }

    OutputStream out = new FileOutputStream(fileName);
    props.store(out, "properties tables");
}

private static File fileName=新文件(“properties.properties”);
Properties props=新属性();
public void writeProperties()引发FileNotFoundException,IOException{
if(fileName.exists()){
load(新文件输入流(文件名));
}
布尔相等=假;
if(!UtilsFunction.isEmpty(props.getProperty(“表列表”)){
字符串[]arr=props.getProperty(“表列表”).split(“,”);

对于(inti=0;i请尝试以下代码。将properties.properties文件保存在src包中

        Properties prop = new Properties();
        try
        {
            // load a properties file from class path, inside static method
            prop.load(ClassNameJava.class.getClassLoader().getResourceAsStream("properties.properties"));

        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }

所以
File#exists
返回
true
而您不能读取它?您有权读取它吗?与JSP(相同目录?)相关的文件在哪里:在Java应用程序中fileName.exists()如果文件确实存在,则始终返回true;而在Jsp中,则始终返回false使用管理员权限运行IDE。私有静态文件文件名=新文件(“properties.properties”);properties props=新属性();这两行保留在静态块中。公共静态文件名=新文件(“src/properties.properties”);我用您的解决方案更改了函数和文件声明;它工作正常;但outputstream上有一个错误。错误为java.io.FileNotFoundException:src\properties.properties(系统找不到指定的路径)
    HTTP Status [500] – [Internal Server Error]
    Type Exception Report

Message java.io.FileNotFoundException: properties.properties (The system cannot find the file specified)

    Description The server encountered an unexpected condition that prevented it from fulfilling the request.

    Exception

    java.io.IOException: java.io.FileNotFoundException: properties.properties (The system cannot find the file specified)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:471)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Root Cause

    java.io.FileNotFoundException: properties.properties (The system cannot find the file specified)
        java.io.FileInputStream.open0(Native Method)
        java.io.FileInputStream.open(Unknown Source)
        java.io.FileInputStream.<init>(Unknown Source)
        packageTableManager.TableExecute.writeProperties(TableExecute.java:233)
        org.apache.jsp.config_jsp._jspService(config_jsp.java:199)
        org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
        org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
        org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Note The full stack trace of the root cause is available in the server logs.

    Apache Tomcat/8.

5.15
        Properties prop = new Properties();
        try
        {
            // load a properties file from class path, inside static method
            prop.load(ClassNameJava.class.getClassLoader().getResourceAsStream("properties.properties"));

        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }