How to open a file for reading in Apache Tomcat fr

2019-08-02 12:10发布

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条回答
Bombasti
2楼-- · 2019-08-02 12:38

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();                
查看更多
登录 后发表回答