getting a relative path of a file
hi,
currenntly i'm reading a html file by giving a the full path of it. But i want to read it giving the realtive path of if. can any one tell me how to give the realtive path of a file in java.
Now its something liek this
BufferedReader fileIn =new BufferedReader(new FileReader("D:\\MyJava\\templates\\tmpUserReport.htm"));
and wish you all a Merry Christmas and a Joyful New Year!!!
[510 byte] By [
Garukaa] at [2007-10-1 1:05:35]

Well, it depends. Relative to what?
If you create File or FileReader without a full path (for instance, just "templates\\tempUserReport.htm") then the parent directory for what you specified will be whatever is indicated by the "user.dir" system property. So if user.dir was "D:\\MyJava" then creating a File or FileReader starting with "templates" would give you the same as what you've got.
Other means of accessing files (URL, getResourceAsStream, etc.) have their own rules as to how the string you pass is interpreted.
Side note: If you prefer, you can use a single forward slash for filenames in Java, even on Windows. "D:/MyJava/templates/tmpUserReport.htm" will also work, and is, IMHO, more readable.
well jverd i'm using this for generate some reports..these are the template files. what i do is read the file put some database values to some specific tags and display it in a textPane. any way i think its totally diffrent story.
my class files are in D:\Myjava\classes and template files are in D:\myJava\templates...only the source files are there in D:\MyJava...so how i suppose to do this.....
> well jverd i'm using this for generate some
> reports..these are the template files. what i do is
> read the file put some database values to some
> specific tags and display it in a textPane. any way i
> think its totally diffrent story.
> my class files are in D:\Myjava\classes and template
> files are in D:\myJava\templates...only the source
> files are there in D:\MyJava...so how i suppose to do
> this.....
If by source files you mean the .java files, then it doesn't matter where they live--unless your program is actually using those files.
You can't really find where your class files are, so you can't find something relative to them, BUT you can do something just as good. Using ClassLaoder.getResourceAsStream() you can find anything that's in your classpath. You don't have to know where exactly it is in your classpath, just where it is relative to any of the roots.
Let's say your classpath is D:/dir1;D:/dir2. You could stick tmpl.txt in either of those locations and then use getResourceAsStream("tmpl.txt"). Or you could stick it in a directory "templates" under either of those two and then use getResourceAsStream("templates/tmpl.txt").
If you're really set on having it relative to a specific class directory (not a good idea) then the only thing I can think of is to provide that directory name as a config option--either from the command line or a properties file or the Preferences API or whatever floats your boat, config-wise.