Reading in from File w/out path name
Does anyone know why netbeans requires an entire path name to read from a file? I am trying to read from a file and I want the program to just look in the directory that the jar file is in to find the file to read from. I do not want the file to be IN the jar file, just inside that directory...
[302 byte] By [
ponchoa] at [2007-11-27 10:14:17]

As I understand it, when your code runs, it does not run relative to the folder that the jar file that contains it exists in. If you want to know where the code is running from in order to use relative path instead of absolute path, put this in your code and examine the output it generates:
File file =new File("");
System.out.println( file.getAbsolutePath() );
Example: if the print out shows your code is running in:
c:/ dir1 /dir2 / dir3
and your file is in
c: / dir2 /dirN / myFile.txt
then your relative path would be:
../dirN/myFile.txt
where starting fromc:/ dir1 /dir2 / dir3,../ means go one folder level "up" (to c:/ dir2 ), and dirN/ means from there, go down into the folder dirN (which would be c: / dir2 /dirN )
I forgot to mention, when you run file.getAbsolutePath() with your code deployed to a server rather than your development enviornment, you will get a different path (you need to try it out). However, your relative path should still work the same.
My problem is that I have a file that is needed to run and I want the program to find it, no matter where the user has it on their system. I want the program to find it in whatever directory the user puts the program in... Is there a way to do that w/out having the user put the file there themselves?
I am confused, can't I just do this:
File path = new File("");
String filename = path.getAbsolutePath() + "NEW SENIORITY LIST BY ALPHA 2.txt"; //NEW SENIORITY LIST BY ALPHA 2.txt being the filename
BufferedReader in = new BufferedReader(new FileReader( filename ));
shouldn't this return the actual path name?
In C++ it's so much less convoluted to do file I/O....
> I am confused, can't I just do this:
Sure, if you want it relative to the present working directory. But pwd has nothing to do with where your class lives. (And if you were going to do it that way, you wouldn't need to bother creating the new File("") first.)
> In C++ it's so much less convoluted to do file I/O....
No, just different.
jverda at 2007-7-28 15:32:24 >
