DEV Community

Jayashree
Jayashree

Posted on

Java File Handling Examples – Logging, File Copy, and Finding Maximum Marks

In this blog, we’ll look at three simple and useful Java programs related to file handling:

  1. Writing logs into separate files (error & log)
  2. Copying content from one file to another
  3. Finding the student with the maximum mark and storing it in a file

These examples are beginner-friendly and help understand how Java works with files.

1. Writing Logs into Separate Files

This program demonstrates how to store messages in different files based on their type.
For example:

  • Error messages go into error.txt
  • Normal logs go into log.txt

Key Idea:

We check the message type and decide the file path accordingly.

Code:

public static void logExample(String type, String message) {
    String filename = "";

    if (type.equalsIgnoreCase("error")) {
        filename = "error.txt";
    } else if (type.equalsIgnoreCase("log")) {
        filename = "log.txt";
    }

    try (BufferedWriter br = new BufferedWriter(new FileWriter(filename, true))) {
        br.write(message);
        br.newLine();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

  • error.txt → stores error messages
  • log.txt → stores normal logs

2. Copying Data from One File to Another

This program reads content from one file and writes it into another file.

Key Idea:

  • Use BufferedReader to read
  • Use BufferedWriter to write

Code:

File inputfile = new File("file1.txt");
File outputfile = new File("file2.txt");

try {
    BufferedReader br = new BufferedReader(new FileReader(inputfile));
    BufferedWriter bw = new BufferedWriter(new FileWriter(outputfile));

    String line;

    while ((line = br.readLine()) != null) {
        bw.write(line);
        bw.newLine();
    }

    br.close();
    bw.close();

    System.out.println("File copied successfully...");
} catch (IOException ioe) {
    ioe.printStackTrace();
}
Enter fullscreen mode Exit fullscreen mode

Output:

Content from file1.txt will be copied into file2.txt.

3. Finding Maximum Mark and Writing to File

This program takes student details (id, name, mark), finds the student with the highest mark, and stores the result in a file.

Key Idea:

  • Store student data in an array
  • Compare marks to find the maximum
  • Write result to file

Code:

Student maxStudent = students[0];

for (Student s : students) {
    if (s.mark > maxStudent.mark) {
        maxStudent = s;
    }
}

try (BufferedWriter writer = new BufferedWriter(new FileWriter("max.txt"))) {
    writer.write("Topper Details:\n");
    writer.write("ID: " + maxStudent.id + "\n");
    writer.write("Name: " + maxStudent.name + "\n");
    writer.write("Mark: " + maxStudent.mark + "\n");
} catch (IOException e) {
    e.printStackTrace();
}
Enter fullscreen mode Exit fullscreen mode

Output:

The file max.txt will contain the details of the student with the highest mark.

Conclusion

These three programs cover important concepts in Java:

  • File writing and appending
  • File reading and copying
  • Basic data processing and storing results

Understanding these basics will help you build more advanced applications like logging systems, report generators, and data processing tools.


Top comments (0)