Skip to content

Topic 06: File Processing

Zhamri Che Ani edited this page May 6, 2026 · 2 revisions

Goal

To introduce students to Java file processing concepts, including reading and writing text and binary files, handling file information, and using Java I/O stream classes for persistent data storage.

🎯 Learning Objectives

Students should be able to:

  1. Explain the purpose and importance of file processing in Java.
  2. Differentiate between text files and binary files.
  3. Use the File class to manage files and directories.
  4. Apply JFileChooser to allow users to select files.
  5. Understand the concept of input and output streams.
  6. Read and write binary files using FileInputStream and FileOutputStream.
  7. Read and write primitive data using DataInputStream and DataOutputStream.
  8. Read and write text files using FileReader, FileWriter, BufferedReader, PrintWriter, and Scanner.
  9. Handle file-related exceptions appropriately.
  10. Develop simple Java applications that perform file operations.

📌 Topics to Cover

1. Introduction to File Processing

  • Persistent data
  • Secondary storage devices
  • File concepts

2. Types of Files

  • Text files
  • Binary files
  • Differences between text and binary storage

3. The File Class

  • Creating File objects
  • Absolute vs relative paths
  • Common file methods:
    • exists()
    • isFile()
    • isDirectory()
    • length()
    • canRead()
    • canWrite()
    • getName()
    • list()

4. JFileChooser

  • Opening file chooser dialog
  • Selecting files and directories

5. Java I/O Streams

  • Input streams
  • Output streams
  • Byte-oriented vs character-oriented streams

6. Binary File Processing

  • FileInputStream
  • FileOutputStream
  • Reading and writing byte arrays

7. Data-Level Binary File Processing

  • DataInputStream
  • DataOutputStream
  • Writing and reading primitive data types

8. Text File Processing

  • FileReader
  • FileWriter
  • BufferedReader
  • PrintWriter
  • Scanner

9. Appending Data to Files

10. Exception Handling in File Processing

  • IOException
  • FileNotFoundException

🛠️ Hands-on Activity

Activity 1: File Information Viewer

Students create a Java program that:

  • Accepts a filename from the user
  • Displays:
    • File name
    • File size
    • Read/write permission
    • Whether it is a file or directory

Activity 2: Write Data to Text File

Students develop a Java application that:

  • Accepts student information from keyboard input
  • Saves the data into a text file using PrintWriter

Example:

Name: Ali
Matric No: 12345
Course: STTPK2023

Activity 3: Read Data from Text File

Students create a program that:

  • Reads all lines from a text file using BufferedReader or Scanner
  • Displays the content on the console

Activity 4: Binary File Processing

Students develop a program that:

  • Stores integer and double values into a binary file
  • Reads the values back correctly using DataInputStream

Activity 5: File Chooser Application

Students create a GUI application using JFileChooser that allows users to:

  • Select a text file
  • Display the selected file path

📚 Suggested Readings

  1. https://docs.oracle.com/javase/tutorial/essential/io/file.html
  2. https://www.w3schools.com/java/java_files.asp
  3. https://www.geeksforgeeks.org/java/file-handling-in-java/
  4. https://www.baeldung.com/java-io-file

Clone this wiki locally