With the experience in programming desktop application,it is straightforward to create a file and read that file,etc.The path problem is trivial.
However,in web application,the path may cause problems.I know if I want to read a file I can use
InputStream is = config.getServletContext().getResourceAsStream("/rates.txt");
is this the only way to solve the path problem when reading a file in web application?
what can I do if I want to write file?How can I define the path for the file?
I have tested to pass a servlet-context-relative path like "/rates.txt" to FileWriter directly,it works fine.It writes something in the file in the web directory.But I donot understand how can the FileWriter find the right path to write file?Does the Tomcat server solve these problems automatically?
If I move the FileWriter out of web application.It just write the file in driver C,liek C:\rates.txt.
How can I define the path for the file? ... But I donot understand how can the FileWriter find the right path to write file? Does the Tomcat server solve these problems automatically?
I'm not sure what you're asking. If you're confused about paths, you might consult the File javadoc. If you're asking about something else, please elucidate. [ August 11, 2006: Message edited by: Rusty Smythe ]
Joined: Apr 27, 2006
I am just confused by the path in web application.
say, using FileWriter in servlet with path "/test.txt" will write a file in servlet-context path directory.
if I move the code out of a web application,it will write test.txt in C:\ .
When you write a file to "/test.txt" you are saying "write a file to the top of the local directory." In windows, that would be C:\ In Linux it is / and in Tomcat, it is the server's home dir ($CATALINA_HOME, or however your container's home is defined).
Suppose your application is deployed to webapps/example. If you save foo.txt as "/webapps/example/foo.txt" it would go into your app's home folder.
Here's some quick 'n' dirty code that demonstrates this:
[ August 11, 2006: Message edited by: Rusty Smythe ]
One thing to keep in mind is that "directories" and "file systems" may not apply in a Java EE webapp. Webapps can be run from an exploded file system. In this case the a call to ServletContext.getRealPath("/") will return the file system path to the root of the web application.
But.... They can also be run from a packed war archive. When run this way, there are no directories because there is no file system. In this case getRealPath will return null.
If you know your app will always be run from a file system, then you can use getRealPath. If you're not sure, or if you want your app to be portable across containers, you should avoid it.
Originally posted by Ben Souther: But.... They can also be run from a packed war archive. When run this way, there are no directories because there is no file system.
And that implies that you CANNOT write a file into your web application. You can't even update a file that is already there. So if your design involves writing into the web app, you should consider doing something different instead.
Joined: Apr 27, 2006
Thanks very much!
In my website,I have to use a .exe file which must be integrated in website to do some process.
So the only way is to build a file system-supported web application in Tomcat.maybe.