• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

need help understanding relative path and absolute path

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2270
20
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");
 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79462
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This forum is awesome, get much faster reply than anywhere

Thanks for sharing, appreciated
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Windows is actually perfectly happy with "/" characters -- neater than using double backslashes...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic