Home / Programs / Reading names and storing them in a text file
Programming Example

Reading names and storing them in a text file

👁 17 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:

Rumman
Ansari
Raja
Rahim
Rocky

Expected Output: names.txt

Rumman
Ansari
Raja
Rahim
Rocky

Program Code

import java.io.*;
public class IO {
    static String fileName = ("names.txt");
    static InputStreamReader isr = new InputStreamReader(System.in);
    static BufferedReader stdin = new BufferedReader(isr);
    public static void main(String[] args) {
        try {
            FileWriter fw = new FileWriter(fileName);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter outFile = new PrintWriter(bw);
            for (int i = 0; i < 5; i++) {
                System.out.print("Enter Name : ");
                String name = stdin.readLine();
                outFile.println(name);
            }
            outFile.close();
        } catch (IOException e) {
            System.err.println(e);
        }
    }
}

Explanation

Java files example
Figure: Java files example

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.