aspose file tools
The moose likes Java in General and the fly likes Check if a path is a directory or a file Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Check if a path is a directory or a file" Watch "Check if a path is a directory or a file" New topic
Author

Check if a path is a directory or a file

Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Hey,

I want to create a FileOutputStream from a String representation of a file on the disk.

Now, if the file does not exist, I want to create it, like so:



Also, the missing directory structure will be created.

My problem now is that when the caller passes a directory rather than a file such as '/root/dir/' the FileOutputStream cannot be created and throws an exception. The missing directories have been created, which I don't want.

So, how do I determine whether or not a file that doesnt exist yet is a directory or not?

thanks


JDBCSupport - An easy to use, light-weight JDBC framework -
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1409

Using java.io.File#isFile() / java.io.File#isDirectory() ?

Edit: Oh, which doesn't exist already, my bad...


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
that doesn't work if the file is not existing.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1409

Sebastian Janisch wrote:that doesn't work if the file is not existing.


Yeah, I sort of only read the subject
Don't suppose a directory path could be assumed to always end neatly on a file separator character?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So, how do I determine whether or not a file that doesnt exist yet is a directory or not?

That question makes no sense. A file that doesn't exist could be anything, including a device, a symlink, anything. Unless you can form the input in a consistent way what you're asking to do is impossible.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

David Newton wrote:
So, how do I determine whether or not a file that doesnt exist yet is a directory or not?

That question makes no sense. A file that doesn't exist could be anything, including a device, a symlink, anything. Unless you can form the input in a consistent way what you're asking to do is impossible.

+1

Sebastian, you can have one little fix in your code that should at least remove any errors:
The parent folder will be created this way without creating a folder for the resource file itself.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Check if a path is a directory or a file
 
Similar Threads
File query
need help understanding relative path and absolute path
The FileOutputStream
Boone #38; File I/O
Writing to a file.