Files

Reading assignment.

Introduction

File class

Files in Unix/Linux

Files in Windows

Remark

You can use static char pathSeparatorChar to get the path seperator.

absolute or relative

Constructors and methods

File

Constructors
Constructors/Methods Description
File(String pathname),
File(String parent, String filename),
File(File directory, String filename)
Creates a new File instance by converting the given pathname string into an abstract pathname. Or creates a new File instance from a parent pathname string (or directory) and a child pathname string.

Test methods
Constructors/Methods Description
exists() Tests whether the file denoted by this abstract pathname exists.
isFile() Tests whether the file denoted by this abstract pathname is a normal file. Returns true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise.
isDirectory() Tests whether the file denoted by this abstract pathname is a directory. Returns true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise.

Action Methods
Constructors/Methods Description
boolean mkdir() Creates the directory named by this abstract pathname. Returns true if and only if the directory was created; false otherwise.
boolean mkdirs() Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Returns true if and only if the directory was created, along with all necessary parent directories; false otherwise
boolean delete() Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted. Returns true if and only if the file or directory is successfully deleted; false otherwise
boolean renameTo(File dest) Renames the file denoted by this abstract pathname. Returns true if and only if the renaming succeeded; false otherwise

List Methods
Constructors/Methods Description
String[] list() Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of strings is returned, one for each file or directory in the directory.
File[] listFiles() Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname. If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned, one for each file or directory in the directory.

Other methods
Constructors/Methods Description
long lastModified() Returns the time that the file denoted by this abstract pathname was last modified.
long length() Returns the length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist

Example

Question 1

Write a program TestExistence.java, such that
java TestExistance file tells you if the file exists or not.
Here are some outputs
C:\myjavaprograms> java TestExistance C:\windows\cookies
C:\windows\cookies exists.
C:\myjavaprograms> java TestExistance abcd\fgh\ijk
abcd\fgh\ijk doesn't exist.

Answer : TestExistence.java


import java.io.*;

public class TestExistence {
    public static void main(String[] args) {
        File file = new File(args[0]);
        if(file.exists()) {
            System.out.println(file + " exists.");
        } else {
            System.out.println(file + " does not exist.");
        }
    }
}
Remark
Try
java TestExistance C:/windows/cookies
java TestExistance C:/windows\cookies

Question 2

Write a method list all the files (and files in the subdirectories) in a given directory.

Answer

    public static void listAllFiles(File file) {
        if(file.isFile()) {
            System.out.println(file);
        } else if(file.isDirectory()) {
            File[] fileList = file.listFiles();
            for(int i = 0; i < fileList.length; i++) {
                listAllFiles(fileList[i]);
            }
        }
    }

Question 3

Write a program to create 3 directories pic10a, pic10b, pic20a under the directory classes.

Answer : Question3.java

import java.io.File;

public class Question3 {
    public static void main(String[] args) {
        String[] dirNames = {"pic10a", "pic10b", "pic20a"};
        for(int i = 0; i < dirNames.length; i++) {
            new File("classes/" + dirNames[i]).mkdirs();
        }
    }
}
Question : do you know the difference between mkdir and mkdirs ?

Question 4

Write a program Question4.java such that java Question4 file shows the size(number of bytes) of the file.

Answer: Question4.java

import java.io.File;

public class Question4 {
    public static void main(String[] args) {
        File file = new File(args[0]);
        if(file.isFile()) {
            System.out.println("The size of the file is " + file.length() + " bytes.");  
        } else {
            System.out.println(file + " is not a file.");
        }
    }
}