Programming Example
Reading names and storing them in a text file
Study this program carefully to understand the logic, output, and explanation in a structured way.
Rumman Ansari Raja Rahim Rocky
Rumman Ansari Raja Rahim Rocky
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);
}
}
}
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.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.