简体   繁体   中英

File path to a resource file inside .war folder

I have to read records from a text file in my REST service method by accessing it by its relative path.

The service has been deployed to JBoss Application Server 7.1.1.

Where should I place the file inside the JBoss directory structure and what would be the best way of accessing resources in this type of a scenario.

I tried placing the file in the root of the war directory which returned java.io.FileNotFoundException

Accessing it through the absolute path worked fine.

Try adding the file to the root of the project and then do the following

// Use resourceAsStream of the classLoader to load the file
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("myFile.properties");

// optionally wrap it with a bufferedReader if the file is large
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

Or if the path of the file is never going to change, you can use absolute paths as follows

FileInputStream in = new FileInputStream("/home/user/file/myFile.properties");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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