Determine path of servlet & file resource
843841Aug 18 2004 — edited Aug 19 2004I've got a servlet that is collecting data from an HTML form, and writing it to a csv file. Right now, I need to feed the servlet this long pathname so that it can find the file I want to write to. The file is kept in the same place as the .class file of the servlet, btw. I thought I could use paths relative to the .class file, but it's not working when I create a new File object. In this same servlet, creating a FileWriter object has no problem spotting a file jsut by passing the file name which is also kpt with the .class file, no path needed. e.g.
Where path is set up as a long string with an absolute path on the server, this works
File f = new File(path, "Test.csv");
p = new PrintWriter(new BufferedWriter(new FileWriter(f, true)));
^^^ I'm then wrapping it in Buffer & Writer and appending to it)
Why does the following work without a path being supplied??
FileWriter fw = new FileWriter("Test.txt");
And, if the answer won't help me, is there some way I can discover the relative path so that when the code is moved to another server, if the path changes it will still work? I've tried getContextPath, getRealPath, etc. on the request object, but none equal the true path. What am I missing?
- Bob