• Post Reply Bookmark Topic Watch Topic
  • New Topic

Relative File Paths

 
Gabriel Cane
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I point a File or FileReader object to a file with a RELATIVE pathname? I can get it to work with an absolute pathnname, but that's not what I want.
I've tried:
filename
\\filename
./filename
/filename
and none of them work.
What do I do?
BTW, I'm developping in Windows, but I will deploy in FreeBSD.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a relative file path, don't you just put the file name without any special leading characters?
By the way, I notice that the examples that you gave of things that you have tried included forward and backward slashes for directory separators. This will make your code platform-specific unnecessarily. The java.io.File class contains a constant called File.separator (I think) that should be used, as it will always contain the right value for the platform on which program runs.
<code>
/* These should work nicely */
String goodAbsPath = File.separator + "myDir" + File.separator + "myFile.txt";
String goodRelPath = "myDir" + File.separator + "myFile.txt";
/* This introduces unnecessary platform dependence */
String badAbsPath = "\myDir\myFile.txt";
</code>
 
Shane Roylance
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the file is in the directory where your code was executed from something like "fin = new FileInputStream("myfile.txt");" should work. But if you are having problems with this you might try using System.getProperty("user.dir"); This should give you the user current working directory.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!