• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help understanding relative path and absolute path  RSS feed

 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone, thanks for reading my post. the books I have or the example I saw so far use
somethine like FileOutputStream fos = new FileOutPutStream ("Joan.emp") which create Joan.emp along with the class file
the FileOutputStream is in. what if I want to create a file outside of the directory the class files reside?

Let's say my class File exist in \Pay_Roll\Action\ and I would like to create Joan.emp in \Pay_Roll\emp\

how would I write the new FileOutputStream part?

Can I write it like FileOutputStream fos = new FileOutputStream("\Pay_Roll\emp\Joan.emp");

if the above is OK? what about

FileOutputStream fos = new FileOutputStream("\Pay_Roll\emp\" + getName() + ".emp");

does the code above I wrote just asking for some sort of Exceptions during runtime?
 
Swastik Dey
Rancher
Posts: 1812
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
back slash in java is treated as an escape character so your code should be as follows

Can I write it like FileOutputStream fos = new FileOutputStream("\Pay_Roll\emp\Joan.emp");
should be assuming I am pay_roll\emp directory is under drice c
FileOutputStream fos = new FileOutputStream("c:\\Pay_Roll\emp\Joan.emp");
if the above is OK? what about

FileOutputStream fos = new FileOutputStream("\Pay_Roll\emp\" + getName() + ".emp");

should be

FileOutputStream fos = new FileOutputStream("c:\\Pay_Roll\\emp\\" + getName() + ".emp");
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always go up one folder by adding .. to the path:

If a path starts with / or \, it is treated as an absolute path on the current partition / drive.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and make sure every \ is written as \\.

Remember it's \ on DOS/Windows and / on Unix Linux and Macs.
 
Davie Lin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This forum is awesome, get much faster reply than anywhere

Thanks for sharing, appreciated
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Windows is actually perfectly happy with "/" characters -- neater than using double backslashes...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!