Open links in new tab
  1. File handling is a crucial aspect of any application, and Java provides several methods for creating, reading, updating, and deleting files. The File class from the java.io package allows us to work with files. To use the File class, you need to create an object of the class and specify the filename or directory name.

    Creating and Working with Files

    To create a file, you can use the createNewFile() method, which returns a boolean indicating whether the file was successfully created. Here is an example:

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

    public class Main {
    public static void main(String[] args) {
    try {
    File myObj = new File("filename.txt");
    if (myObj.createNewFile()) {
    System.out.println("File created: " + myObj.getName());
    } else {
    System.out.println("File already exists.");
    }
    } catch (IOException e) {
    System.out.println("An error occurred.");
    e.printStackTrace();
    }
    }
    }
    Copied!

    Reading Files

    To read a file, you can use the Scanner class to read the contents of the file. Here is an example:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class Main {
    public static void main(String[] args) {
    try {
    File myObj = new File("filename.txt");
    Scanner myReader = new Scanner(myObj);
    while (myReader.hasNextLine()) {
    String data = myReader.nextLine();
    System.out.println(data);
    }
    myReader.close();
    } catch (FileNotFoundException e) {
    System.out.println("An error occurred.");
    e.printStackTrace();
    }
    }
    }
    Copied!
  1. File (Java Platform SE 8 ) - Oracle Help Center

    Learn how to use the File class in Java to create, access, and manipulate files and directories. The File class provides methods to test permissions, create temporary files, and convert pathnames to …

  2. File Handling in Java - GeeksforGeeks

    Mar 13, 2026 · In Java, file handling means working with files like creating them, reading data, writing data or deleting them. It helps a program save and use …

  3. Java File (With Examples) - Programiz

    Learn how to use the File class of the java.io package to perform various operations on files and directories. See examples of creating, reading, writing and deleting files using File, FileReader, …

  4. Java File Class - Tpoint Tech

    Feb 12, 2026 · In this chapter, we will learn what the File class is, why it is used, its declaration, fields, constructors, methods, and multiple examples to clearly understand how file and directory operations …

  5. Java file - working with files in Java - ZetCode

    Jan 27, 2024 · Java file tutorial shows how to work with files in Java. We create a file, read from a file, write to a file, move a file with Java Files.

  6. The Java File Class - Baeldung

    Feb 8, 2025 · In this tutorial, we’ll give an overview of the File class, which is part of the java.io API. The File class gives us the ability to work with files and directories on the file system.

  7. Java - File Class - Online Tutorials Library

    Java provides several methods for file handling like creating, reading, updating and deleting the files. In Java, the File class is used to reprsent the path of a file or a …

  8. Java File I/O: Complete Guide to Reading and Writing Files

    Master Java File I/O with this comprehensive guide. Learn file reading, writing, and manipulation techniques with practical examples for beginners and professionals.

  9. java - How do I create a file and write to it? - Stack Overflow

    best way is to use Java7: Java 7 introduces a new way of working with the filesystem, along with a new utility class – Files. Using the Files class, we can create, move, copy, delete files and directories as …

  10. People also ask
    Loading
    Unable to load answer