Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which folder is FileInputStream looking?

Tags:

java

tomcat

I use the line

FileInputStream stream = new FileInputStream("myfile.properties");

to open a properties file without specifying a path.

When running it on Apache Tomcat, the file can not be found. I placed the file into the root folder of the application.

In which folder is Java looking?

I can not change the path because the code is not by me.

like image 605
Alex Avatar asked May 02 '11 10:05

Alex


People also ask

How do I find a file from FileInputStream?

int read() − This simply reads data from the current InputStream and returns the read data byte by byte (in integer format). This method returns -1 if the end of the file is reached. int read(byte[] b) − This method accepts a byte array as parameter and reads the contents of the current InputStream, to the given array.

How do you put a file path in FileInputStream?

This String should contain the path in the file system to where the file to read is located. Here is a code example: String path = "C:\\user\\data\\thefile. txt"; FileInputStream fileInputStream = new FileInputStream(path);

What is FileInputStream in selenium?

A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader .

What is difference between file and FileInputStream?

The main difference between the FileReader and FileInputStream is that one reads data from a character stream while the other reads data from a byte stream. The FileReader automatically converts the raw bytes into characters by using the platform's default character encoding.


2 Answers

It will look in the folder that is is the result of

System.getProperty("user.dir")

and this could be anywhere. That depends the current working directory when the tomcat server is started.

Try to load a small servlet that prints that info at loading if you cannot figure out from standard logs or the tomcat start procedure.

like image 165
PeterMmm Avatar answered Oct 17 '22 00:10

PeterMmm


It will look in the working directory of the JVM process, not the root directory of the WAR. Where that working directory is depends on how the Tomcat process was started.

As such, you shouldn't do this. You should obtain references to resources from inside the WAR by asking the ServletContext object (which has various methods to look up resource streams), e.g. from inside a Servlet:

InputStream stream = getServletContext().getResourceAsStream("myfile.properties");

Also, it's bad practice to refer to resources inside a WAR as actual files. This will only work if the WAR is exploded into a directory structure, and won't work if the servlet contain decided to run the WAR as an un-exploded .WAR file. By sticking to the getResource...() methods, you keep things neutral and portable.

However if, as you say, you cannot change the code, then that's a problem, because the code is broken and badly written. You'll need to figure out how to launch Tomcat so that the working directory is in the "correct" place. That might entail hacking the startup scripts.

like image 31
skaffman Avatar answered Oct 16 '22 23:10

skaffman