It's not a secret anymore!
The moose likes Beginning Java and the fly likes Creating a file by giving relative path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating a file by giving relative path" Watch "Creating a file by giving relative path" New topic

Creating a file by giving relative path

Krishna Unni

Joined: Jun 01, 2006
Posts: 5
How can i create a file by giving 'relative path' as parameter like '../Logging/note.log' ?.
Fahd Shariff
Ranch Hand

Joined: Nov 22, 2002
Posts: 38
File file = new File("../Logging/note.log") ;

Fahd Shariff<br />"Let the code do the talking"
marc weber

Joined: Aug 31, 2004
Posts: 11343

"Krishna T K,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name. First names can be initials, but last names cannot.

You can edit your display name here.

Thank you for your prompt attention, and enjoy the ranch!


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

The problems I see with using relative paths are -
1) You can't be sure where the files are being created since it depends on how the application is invoked.
2) You may not have permission to write to the directory.

I try to create files in directories that I know I have permission. For example System.getProperty("user.home") is, as the name suggests, the user 'home' directory and one should be able to assume that a process running as a particular user can create files (or directories) in that user's home directory. If one is in any doubt then one can always use File.canWrite() .

If you just want a temporary file then one of the File.createTempFile(...) methods can be used or you can look use the system temporary directory which can be obtained from System.getProperty("").

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Krishna --

Please try again with the name. You need two names, first and last. No initials for the last name. Thanks.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Creating a file by giving relative path
It's not a secret anymore!