Practice Programs on File operations

1. Create a New File

This program demonstrates how to create a new file using the File class. If the file already exists, an appropriate message is displayed.

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

public class CreateFileExample {

    public static void main(String[] args) throws IOException {

        File file = new File("Student.txt");

        if (file.createNewFile()) {
            System.out.println("File created successfully.");
        } else {
            System.out.println("File already exists.");
        }
    }
}
Output:
File created successfully.

2. Write Data to a File

This program writes text into a file using the FileWriter class. If the file does not exist, it is created automatically.

import java.io.FileWriter;
import java.io.IOException;

public class WriteFileExample {

    public static void main(String[] args) throws IOException {

        FileWriter writer = new FileWriter("Student.txt");

        writer.write("Welcome to Java File Handling");
        writer.write("\nLearning File Operations");

        writer.close();

        System.out.println("Data written successfully.");
    }
}
Output:
Data written successfully.

File(Student.txt) Data:
Welcome to Java File Handling
Learning File OperationsCode language: CSS (css)

3. Read Data from a File

This program reads data from a file using the FileReader class. Each character is read one by one and displayed on the console.

import java.io.FileReader;
import java.io.IOException;

public class ReadFileExample {

    public static void main(String[] args) throws IOException {

        FileReader reader = new FileReader("Student.txt");

        int ch;

        System.out.println("File Contents:");

        while ((ch = reader.read()) != -1) {
            System.out.print((char) ch);
        }

        reader.close();
    }
}
Output:
File Contents:
Welcome to Java File Handling
Learning File Operations

4. Append Data to a File

This program appends new text to an existing file. The existing file contents remain unchanged while new data is added at the end.

import java.io.FileWriter;
import java.io.IOException;

public class AppendFileExample {

    public static void main(String[] args) throws IOException {

        FileWriter writer = new FileWriter("Student.txt", true);

        writer.write("\nJava Programming Lab");

        writer.close();

        System.out.println("Data appended successfully.");
    }
}
Output:
Data appended successfully.

File(Student.txt) Data:
Welcome to Java File Handling
Learning File Operations
Java Programming LabCode language: CSS (css)

5. Display File Information

This program displays important information about a file. It shows the file name, path, size, and read/write permissions.

import java.io.File;

public class FileInformation {

    public static void main(String[] args) {

        File file = new File("Student.txt");

        if (file.exists()) {

            System.out.println("File Name : " + file.getName());
            System.out.println("File Path : " + file.getAbsolutePath());
            System.out.println("File Size : " + file.length() + " bytes");
            System.out.println("Readable : " + file.canRead());
            System.out.println("Writable : " + file.canWrite());

        } else {

            System.out.println("File does not exist.");
        }
    }
}
Output:
File Name : Student.txt
File Path : D:\Testing\Student.txt
File Size : 75 bytes
Readable : true
Writable : trueCode language: JavaScript (javascript)

6. Rename a File

This program renames an existing file using the renameTo() method. If the operation is successful, the new file name is displayed.

import java.io.File;

public class RenameFileExample {

    public static void main(String[] args) {

        File oldFile = new File("Student.txt");
        File newFile = new File("StudentDetails.txt");

        if (oldFile.renameTo(newFile)) {

            System.out.println("File renamed successfully.");

        } else {

            System.out.println("Unable to rename the file.");
        }
    }
}
Output:
File renamed successfully.

7. Delete a File

This program deletes an existing file using the delete() method. A confirmation message is displayed after the deletion.

import java.io.File;

public class DeleteFileExample {

    public static void main(String[] args) {

        File file = new File("StudentDetails.txt");

        if (file.delete()) {

            System.out.println("File deleted successfully.");

        } else {

            System.out.println("Unable to delete the file.");
        }
    }
}
Output:
File deleted successfully.

References

CodeGPT-small-java
Codebert-base
CodeBERT
Scroll to Top