Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes File class questoin Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File class questoin" Watch "File class questoin" New topic

File class questoin

haijun wang

Joined: Aug 18, 2000
Posts: 21
Hi My friends:
I am trying to use JAVA file class to create a directory and file using following code in windows ME environment. The compile is ok, but it looks like both directory and file were not created. Could you please tell me the reason.
Thank you
public class TryFile
public static void main (String [ ] args)
File myDir = new File ("D:/newFile/newFile/try");
System.out.println (myDir + (myDir.isDirectory()? " is" :" is not") + " a directory.");
File myFile = new File ("");
System.out.println ( myFile + (myFile.exists() ? " does" : " does not" ) + "exist");
System.out.println ("you can" + (myFile.canRead() ? " " : " not ") + "read " + myFile);
System.out.println("you can" + (myFile.canWrite()? " ": " not ") + "write" + myFile );
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
The reason is simple: Java File class doesn't create any files EVER!!
You must use other io to create files. For example:
RandomAccessFile raf = new RandomAccessFile( "newFile", "rw" );
creates a file named "newFile" and opens it for reading and writing. Another example:
FileInputStream fis = new FileInputStream( "newFile" );
creates a file named "newFile" and opens it for reading. Yet another example:
FileOutputStream fos = new FileOutputStream( "newFile" );
creates a file named "newFile" and opens it for writing.
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
Yeah, the File class is a bit deceptively named - it's just the name of a file/directory, nothing more. It's useful as it lets you find out / set stuff about the file with the name stored in the File object, but it's not actually a file as in a collection of bits on the HD.
I think you can actually create a file with the createNewFile() or createTempFile() methods of File, like

but I've never actually tried it.
I agree. Here's the link:
subject: File class questoin
It's not a secret anymore!