Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java file


May 10, 2021 Java


Table of contents


Java IO Tutorial - Java File


The object of the File class is an abstract expression of the path name of a file or directory.

Create a file

We can create a File it

  • The path name
  • The parent path name and the child path name
  • URI (Unified Resource Identifier)

We can create a file using one of the following constructors of the File class:

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

If we have a file path name string test .txt, we can create an abstract path name as the following code.

File dummyFile = new File("test.txt");

A file named test .txt does not have to exist to use this statement to create a File object.

The dummyFile object represents an abstract path name that may or may not point to a real file in the file system.

The File class has several ways to work with files and directories.

With File objects, we can create new files, delete existing files, rename files, change permissions on files, and more.

IsFile() and isDirectory () in the File class tell File objects whether they represent files or directories.


The current working directory

The current working directory of the JVM is based on how we run java commands.

We can get the current working directory of the JVM by reading the user.dir system properties, as follows:

String  workingDir = System.getProperty("user.dir");

Use the System.setProperty() method to change the current working directory.

System.setProperty("user.dir", "C:\\myDir");

To specify the C:\test as the user.dir system property value on Windows, we run a program like this:

java -Duser.dir=C:\test your-java-class

The existence of the file

We can use the File class's exists() method to check for the existence of the abstract path name of the File object.

boolean fileExists = dummyFile.exists();

Full source code

import java.io.File;

public class Main {
  public static void main(String[] argv) {
    // Create a File object
    File dummyFile = new File("dummy.txt");

    // Check for the file"s existence
    boolean fileExists = dummyFile.exists();
    if (fileExists) {
      System.out.println("The dummy.txt  file exists.");
    } else {

      System.out.println("The dummy.txt  file does  not  exist.");
    }

  }
}

The code above produces the following results.

Java file

Path

The absolute path uniquely identifies the file on the file system. The specification path is the simplest path to identify files on the file system.

We can use the getAbsolutePath() and getCanonicalPath() methods to obtain absolute and canonical paths represented by file objects, respectively.

import java.io.File;
import java.io.IOException;

public class Main {
  public static void main(String[] args) {
    printFilePath("dummy.txt");
    printFilePath(".." + File.separator + "notes.txt");
  }

  public static void printFilePath(String pathname) {
    File f = new File(pathname);
    System.out.println("File  Name: " + f.getName());
    System.out.println("File  exists: " + f.exists());
    System.out.println("Absolute Path: " + f.getAbsolutePath());

    try {
      System.out.println("Canonical Path: " + f.getCanonicalPath());
    }

    catch (IOException e) {
      e.printStackTrace();
    }
  }
}

The code above produces the following results.

Java file

File separator

Different operating systems use different characters to separate two parts of the path name.

For example, Windows uses a backslash as a name separator in a path name, while UNIX uses a forward slash (/).

The File class defines a constant named separator Char, which is a system-related name separator.

We can use the File.separator Char constant to get the name separator as a character.

File.separator constants use our name separator as String.

Using a name separator in a program will make your Java code work on different platforms.