In a JSP project I am reading a file from directory. If i give the full path then i can easily read the file
BufferedReader br = new BufferedReader(new FileReader("C:\\ProjectFolderName\\files\\BB.key"));
but i don't want to write the full path instead i just want to give the folder name which contains the file, like bellow.
BufferedReader br = new BufferedReader(new FileReader("\\files\\BB.key"));
How to do this?
String currentDirectory = new File("").getAbsolutePath();
System.out.println(currentDirectory);
BufferedReader br = new BufferedReader(new FileReader(currentDirectory + "\\files\\BB.key"));
I tried the above still cant read from file
the print line gives the following output
INFO: C:\Program Files\NetBeans 7.3
Use
File file = request.getServletContext().getRealPath("/files/BB.key");
This translates URL paths relative (hence '/') from the web contents directory to a file system File.
For a portable web application, and knowing the file is in Windows Latin-1, explicitly state the encoding, otherwise the default OS encoding of the hoster is given.
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "Windows-1252"));
If the file is stored as resource, under /WEB-INF/classes/ you may also use
BufferedReader br = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream("/files/BB.key"), "Windows-1252"));
In that case the file would reside under /WEB-INF/classes/files/BB.key.