// Import the java.io package to make the File class // available to you without having to type the package // name in front of the class name each time. import java.io.*; public class TestFileClass { public static void main(String[] args) { // Creates a reference to a file object. File aFile = new File("data.txt"); // Creates a file object to use as // a directory name File aDir = new File("mydirectory"); // Checks if the file exists and that // it is a file. If so the name and path // are printed to the screen. if(aFile.exists() && aFile.isFile()) System.out.println(aFile.getName() + " exists and is located at " + aFile.getAbsolutePath()); else System.out.println(aFile + "Does not exist."); if(aFile.canWrite()) System.out.println(aFile + " can be written to."); // Sets the file to read only. aFile.setReadOnly(); System.out.println(aFile + "is now read only."); // Creates a directory, giving it the // object name. The if statement tests // if the directory is successfully // created. aDir.mkdir(); if (aDir.isDirectory()) System.out.println(aDir.getName() + " is a directory."); else System.out.println("Directory was not created."); } }