|  
              Files and directories are accessed and manipulated 
                via the java.io.File 
                class. The File 
                class does not actually provide for input and output to 
                files. It simply provides an identifier of files and directories. 
               Always remember that just because a File 
                object is created, it does not mean there actually exists on the 
                disk a file with the identifier held by that File 
                object.   
              
              
              The File 
                class includes several overloaded constructors. For example, the 
                following instance of File 
                refers to a file named myfile.txt 
                in the current directory of the program that the JVM is running: 
                File 
              file = new File ("myfile.txt");   
              Again, the file myfile.txt 
                may or may not exist in the file system. An attempt to use File 
                object that refers to a file that does not exist will cause a 
                FileNotFoundException to be thrown. The File 
                instance can also created with a filename that includes path:  File 
              fileA = new File("/tmp/filea.txt"); 
                
              Another overloaded constructor allows separate specification 
                of the path and the file name:   File 
                fileA = new File("/tmp", "filea.txt"); 
                 Directories can also be specified:   File 
                direc = new File("/tmp"); 
                 There are a number of useful methods in File, 
                e.g.:  
              
                Boolean 
                  exist(); 
                        - does the file existBoolean canWrite();    - can the file be 
                  written to
 Boolean canRead();     - can the file 
                  be read
 Boolean isFile();      - does it represent 
                  a file
 Boolean isDirectory(); - or a directory
 
              There are also methods to get the file name and path components, 
                to make a directory, to get a listing of the files in a directory, 
                etc.  
               
                String getName 
                  () - get the name of the file (no path included)String getPath () - get the abstract file path
 String getCanonicalPath () - get the name of the file with path
 String getAbsolutePath () - get the absolute file path
  
              Note that path names use different separator characters 
                on different hosts. Windows uses "\", Unix"/", 
                Macintosh ":". The static variables:  File.separator 
              - string 
              with file separatorFile.separatorChar 
              - char with file separator
 File.pathSeparator - string 
              with path separator
 File.pathSeparatorChar - 
              char with path separator
  
              can be used to insure that your programs are platform 
                independent. For example, this snippet shows how to build a platform 
                independent path:  String 
              dirName = "dataDir";String filename = "data.dat";
 File filData = new File(dirName + File.separator + filename);
  
              Other talents of the File 
                class include the method   boolean 
              mkdir ()  
              This method will create a directory with the abstract 
                path name represented by the File 
                object if that File 
                object represents a directory. The method returns true 
                if the directory creation succeeds and false 
                if not. A situation in which the directory cannot be created is, 
                for example, when the File 
                object refers to an actual file that already exists in the file 
                system.  References & 
                Web Resources   Latest update: Nov. 12, 2004 |