File Class Part One Tutorial

The File class has been around since the very beginning of Java and it is part of the java.io package. The File class contains methods and constructors for various file and directory manipulating features.
Java is platform independent; your Java programs can execute on any operating system that the JVM can run on. That doesn't mean that the JVM is independent of the rules of the file system that the JVM is running on. As a matter of fact, the JVM and your programs must be written in such a way that they can dynamically adapt to the OS that they are being run on.
Consider this Windows hard coded path represented as a string: "c:\\Java\\BW\\Sample.txt". If a user attempted to run your program on a Linux or UNIX OS, your program will fail miserably. That is because the directory separator is '/' in UNIX as opposed to '\' in Windows. The file class provides us with several tools to dynamically create our directory and file structure. On a side note, in Java 7, a new class named Files was introduced to provide greater flexibility and functionality for supporting dynamic directory and file capabilities. I will be creating a tutorial on that class in the future. Don't be tempted to skip over learning the File class as it is necessary to understand some core concepts before tackling the Files class.

Constructors:
File(File parent, String child)
File(String pathname)
File(String parent, String child)
File(URI uri)



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md FilePartOne
C:\Java>cd FilePartOne
C:\Java\FilePartOne>Notepad FilePartOne.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.io.*;
class FilePartOne {
    public static void main(String args[]) {
        System.out.println("Let's see the values of the File class 4 static fields (properties):");
        System.out.println("pathSeparator = " + File.pathSeparator); // returns a String
        System.out.println("pathSeparatorChar = " + File.pathSeparatorChar); // returns a char
        System.out.println("separator = " + File.separator); // returns a String
        System.out.println("separatorChar = " + File.separatorChar);// returns a char
        System.out.println();

        System.out.println("Test the constructors:");
        File myDir = new File(File.separator);
        System.out.println("myDir.getAbsolutePath() = " + myDir.getAbsolutePath());
        System.out.println("myDir.isDirectory() = " + myDir.isDirectory());
        System.out.println("myDir.isFile() = " + myDir.isFile());
        System.out.println();

        myDir = new File(File.separator+"Java"+File.separator+"FilePartOne");
        System.out.println("myDir.getAbsolutePath() = " + myDir.getAbsolutePath());
        System.out.println("myDir.isDirectory() = " + myDir.isDirectory());
        System.out.println("myDir.isFile() = " + myDir.isFile());
        System.out.println();

        File myFile = new File(myDir, "Temp.txt");
        System.out.println("myFile.getAbsolutePath() = " + myFile.getAbsolutePath());
        System.out.println("myFile.isDirectory() = " + myFile.isDirectory());
        System.out.println("myFile.isFile() = " + myFile.isFile());
        System.out.println("myFile.exists() = " + myFile.exists());
        try { 
            myFile.createNewFile(); 
        } catch (IOException e) { 
            System.out.println(e.getMessage()); 
        }
        System.out.println("After myFile.createNewFile()"); 
        System.out.println("myFile.exists() = " + myFile.exists());
        System.out.println("myFile.isFile() = " + myFile.isFile());
        myFile.delete();
        System.out.println("After myFile.delete()");
        System.out.println("myFile.exists() = " + myFile.exists());
        System.out.println();
	
        try (BufferedWriter bw = new BufferedWriter(new FileWriter(myFile))) {
            bw.write("Creating a File object dynamically is the way to go.");
            bw.newLine();
            bw.write("It really isn\'t that hard to do!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        try (BufferedReader br = new BufferedReader(new FileReader(myFile))) {
            System.out.println("The contents of " + myFile.getAbsolutePath());
            while(br.ready()) {
                System.out.println(br.readLine());
            }		
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        String entireName = File.separator+"Java"+File.separator+"FilePartOne"+File.separator+"Sample.txt";        
        try (BufferedWriter bw = new BufferedWriter( new FileWriter(new File(entireName)) ) ) {
            bw.write("Another nice thing about dynamically building your path is that you don\'t have to");
            bw.newLine();
            bw.write("hard code \\\\ backslash characters in a huge string literal!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Now switch back to the command prompt (CMD) and type in javac FilePartOne.java and press Enter.
Now type in java FilePartOne and press Enter.


C:\Java\FilePartOne>javac FilePartOne.java
C:\Java\FilePartOne>java FilePartOne
Let's see the values of the File class 4 static fields (properties):
pathSeparator = ;
pathSeparatorChar = ;
separator = ;
separatorChar = ;

Test the constructors:
myDir.getAbsolutePath() = C:\
myDir.isDirectory() = true
myDir.isFile() = false

myDir.getAbsolutePath() = C:\Java\FilePartOne
myDir.isDirectory() = true
myDir.isFile() = false

myFile.getAbsolutePath() = C:\Java\FilePartOne\Temp.txt
myFile.isDirectory() = false
myFile.isFile() = false
myFile.exists() = false
After myFile.createNewFile()
myFile.isFile() = true
myFile.exists() = true
After myFile.delete()
myFile.exists() = false

The contents of C:\Java\FilePartOne\Temp.txt
Creating a File object dynamically is the way to go.
It really isn\'t that hard to do!


Final thoughts

None


Tutorials