Creating Files Using Class File : File « File « SCJP






import java.io.*;              // The Java 6 exam focuses on
                               // classes from java.io
class Writer1 {
  public static void main(String [] args) {
    File file = new File("fileWrite1.txt");    // There's no
                                               // file yet!
  }
}


import java.io.*;

class Writer1 {
 public static void main(String [] args) {
   try {                        // warning: exceptions possible
     boolean newFile = false;
     File file = new File                // it's only an object
                    ("fileWrite1.txt");
     System.out.println(file.exists());  // look for a real file
     newFile = file.createNewFile();     // maybe create a file!
     System.out.println(newFile);        // already there?
     System.out.println(file.exists());  // look again
   } catch(IOException e) { }
 }
}








9.1.File
9.1.1.java.io.File class represents the name of a file or directory that might exist on the file system.
9.1.2.File: boolean exists() Returns true if the file or directory exists; otherwise returns false.
9.1.3.File: String getAbsolutePath() Returns the absolute (that is, not relative) path of the file or directory.
9.1.4.File: String getCanonicalPath() Returns the canonical path of the file or directory.
9.1.5.File: String getName() Returns the name of the file or directory.
9.1.6.File: String getParent() Returns the name of the directory that contains the File.
9.1.7.File: boolean isDirectory() Returns true if the File object describes a directory that exists on the file system.
9.1.8.File: boolean isFile() Returns true if the File object describes a file that exists on the file system.
9.1.9.File: String[] list() Returns an array of the files and directories within the File instance.
9.1.10.File: boolean canRead() Returns true if the file or directory may be read.
9.1.11.boolean canWrite() Returns true if the file or directory may be modified.
9.1.12.boolean createNewFile() Creates a new empty file
9.1.13.boolean delete() Attempts to delete the file or directory.
9.1.14.long length() Returns the length of the file.
9.1.15.boolean mkdir() Attempts to create a directory whose path is described by the current instance of File.
9.1.16.boolean renameTo(Filenewname) Renames the file or directory.
9.1.17.Creating Files Using Class File