Understanding Java File Operations

Input/Output and Streams in Java

Alex Liu

Software Development Engineer

About Alex Liu

$$

  • M.S. Degree in Computer Science

$$

  • 8 years of experience in the software development industry

$$

  • 6 years of hands-on expertise in Java programming

A cartoon avatar of the instructor

Input/Output and Streams in Java

Course overview

Screenshot 2025-05-20 at 15.47.25.png

                  Java File Operations
                           Creation
                            Deletion
                Directory management

Input/Output and Streams in Java

Course overview

Screenshot 2025-05-20 at 15.47.18.png

                  Java File Operations        Iterators and Streams
                           Creation                   Processing collections
                            Deletion                     Transforming data
                Directory management

Input/Output and Streams in Java

Course overview

Screenshot 2025-05-20 at 15.47.09.png

                  Java File Operations        Iterators and Streams      Custom Methods and More
                           Creation                   Processing collections               Build scalable                             Deletion                     Transforming data                 Java applications
                Directory management

Input/Output and Streams in Java

Creating files

  • Import the File class

    import java.io.File;
    
  • Make a File object named dataTextFile

    File dataTextFile = new File("data.txt");
    
  • To create the file on the computer, call the .createNewFile() method

    boolean result = dataTextFile.createNewFile();
    
  • Returns true if the file is created successfully; false if it already exists
  • Works for any file type
Input/Output and Streams in Java

Deleting files

  • Create a File object named exampleFile that references the file we want to remove

    File exampleFile = new File("example.txt");
    
  • Use the .delete() method to attempt to delete the file

    boolean deleted = exampleFile.delete();
    
  • Returns true if the file was deleted
  • Returns false if file could not be deleted (e.g., due to insufficient permissions)
Input/Output and Streams in Java

Checking file existence

  • The .exists() method returns true if the file exists
// Use `.exists()` to check if a file already exists on our disk
if (dataTextFile.exists()) {
    // Print message if file exists
    System.out.println("The file already exists.");
} else {
    // Attempt to create the file if it doesn't exist
    boolean result = dataTextFile.createNewFile();
}
  • Helps prevent duplicates
Input/Output and Streams in Java

Wrapping file operations with try-catch

  • Always wrap file operations in a try-catch block
    • Handles potential exceptions like IOException
      • From permission errors or invalid files
try {
    //Any file operation
} catch (IOException e) {
    e.getMessage();
}
Input/Output and Streams in Java

Let's practice!

Input/Output and Streams in Java

Preparing Video For Download...