How to open a file for reading in Apache Tomcat fr

2019-08-02 12:29发布

问题:

I am using the Apache Tomcat server. In a Java class in a library, I need to open a file to read it using getResourceAsStream(). Which directory should the file be in so it can be opened from the Java code, and what path should I use in the argument to getResourceAsStream()?

回答1:

This code reads test.properties file from the conf folder:

            File file = null;
            if (System.getProperty("catalina.base") != null)
                file = new File(System.getProperty("catalina.base") + "/conf/test.properties");
            else
                throw new RuntimeException("Catalina.base doesn't exists.");
            FileInputStream fis = new FileInputStream(file);
            props.load(fis);
            fis.close();