Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a file by giving relative path

 
Krishna Unni
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i create a file by giving 'relative path' as parameter like '../Logging/note.log' ?.
 
Fahd Shariff
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File file = new File("../Logging/note.log") ;
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!

-Marc
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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("java.io.tmpdir").
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Krishna --

Please try again with the name. You need two names, first and last. No initials for the last name. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic