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 Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 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

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.