Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Files in Java

 
Cecil Phillip
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I noticed in Java is the if I declare a File oject and initialize it to a File, lets say
File test = new File("C:\java","testfile.java");
When I used a BufferedReader to write to the File object, the actual file (testfile.java) will be created. However if I initialize it with a directory that doesn't exist an exception is thrown. What I want to know is why is that the file would be created if it doesn't exist, but the direcctory wouldn't? Also how could I create that directory?
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class File has a method called mkdir()...
 
Adam Blinkinsop
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Cecil Phillip:
What I want to know is why is that the file would be created if it doesn't exist, but the direcctory wouldn't?

java.io.File objects aren't really files, but filenames. You can create any legal filename you want, it doesn't have to exist (see the 'exists()' method in the File class).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic