Title: File Class
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

File Class

The File class provides many capabilities related to directories and file management. To see a complete list, go to online help. The following list provides some of the more common File characteristics. The FileManager.java program below demonstrates using some of these methods to implement a small version of the standard version of Unix commands.

One confusion is that the File class is used for files and directories. If you want to know if your class is a directory or a an actual file, call the isDirectory() method.

Constant:
File.separator - Constant string which takes on the value of "/" on Unix and "\" on Windows

- Constructors:
File(String pathname) - Creating a File class does not imply a file on the disk. The pathname string is a complete absolute pathname
- File(String dirPath, String name) - Create the full path by gluing the dirPath to the name separated by File.separator
- File (File dirParent, String name) - Some as the previous constructor, except that the directory is contained in the File dirParent class.

Methods:

- list() - Works on a File instance that happens to be a directory. returns an array of Strings containing the names of the files in the directory.
- Date lastModified() - Returns the Date of the last modification to this File
- boolean renameTo(File fnewName) - Provides a way to rename a filename
- boolean createNewFile() - Used to take a File class and make a real file out of it. If the file doesn't already exist, a file with zero bytes is created. If the file does exist, this operation will change the lastModified time on the file. This method feels a lot like the Unix touch command.
- boolean mkdir() - Create a new directory
- boolean delete() - delete a file
- boolean exists() - returns a boolean to indicate whether the file exists or not
- boolean canRead() - returns whether the File class is readable
- boolean canWrite() - returns whether the File class is writeable
- String getName() - returns the filename without the path information
- String getAbsolutePath() - returns the full pathname of the File class.

Something to Try: - See if you can do the following things in Java:

- Make a directory.
- Create a File in the directory (write a program using any method described above that you want)
- Rename the File
- Copy the File to another File (write a program using any methods described above that you want)
- Delete the First File
- Print out the contents of the copied file (write a program using any methods described above that you want)

Sample Program created in the associated video:


import java.io.File;
import java.util.Date;


public class File_Examples {

    public static void main(String[] args) {

        File f = new File("abc.txt");

        if (f.isDirectory())
            System.out.println("f is a Directory");
        else
            System.out.println("f is NOT a Directory");

        if (f.exists())
            System.out.println("abc.txt exists");
        else
            System.out.println("abc.txt Does NOT exist");

        File dir = new File("MyDir");
        if (dir.mkdir())
            System.out.println("MyDir was created");
        else
            System.out.println("Couldnt create MyDir");

        if (dir.isDirectory())
            System.out.println("dir is a Directory");
        else
            System.out.println("dir is NOT a Directory");

        System.out.println("getName returns: "+ dir.getName());
        System.out.println("getAbsolutePath returns : " + dir.getAbsolutePath());

        File dirNew = new File("NewMyDir");
        dir.renameTo(dirNew);

        File fileFNew = new File ("NewMyDir" + File.separator + "abc.txt");
        f.renameTo(fileFNew);

        File fDelete = new File("xyz.txt");
        fDelete.delete();

        File fstatic = new File("staticFile.txt");

        long time = fstatic.lastModified();
        Date d = new Date(time);
        System.out.println("last modified ="+ d);

        long size = fstatic.length();
        System.out.println("length ="+ size);

        File currWorkingDirectory = new File(".");

        String[] fileNames = currWorkingDirectory.list();
        for (int i=0; i < fileNames.length; i++)
            System.out.println(fileNames[i]);


    }

}



Last Updated: July 29, 2013 4:49 PM