Repository navigation
Expand file tree
/
Copy pathJ15_08.java
More file actions
97 lines (96 loc) · 2.39 KB
/
Copy pathJ15_08.java
File metadata and controls
97 lines (96 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package chapter15;
/* J15_08.java */
/* Writing and Reading Objects in Data File */
import java.io.*;
import java.util.*;
class Student{
int Roll;
String Name;
double Mark;
public Student( int R, String N, double M) {
Roll = R;
Name = N;
Mark = M;
}
public Student() // Empty Constructor
{
}
public void WriteData(PrintWriter PR) throws IOException
{
PR.println(Roll + "|" + Name + "|" + Mark); // | as delimiter
}
public void ReadData(BufferedReader in) throws IOException
{
String Str = in.readLine();
StringTokenizer t = new StringTokenizer(Str, "|");
Roll = Integer.parseInt(t.nextToken());
Name = t.nextToken();
Mark = Double.parseDouble(t.nextToken());
}
public void display()
{
System.out.println( Roll + " " + Name + " " + Mark);
// Blank Space (" ") as delimiter
}
}
public class J15_08{
static void WriteData(Student[] S, PrintWriter PR)
throws IOException
{
PR.println(S.length);
int i;
for (i = 0; i < S.length; i++)
{
S[i].WriteData(PR);
}
}
static Student[] ReadData(BufferedReader BR) throws IOException
{
int n = Integer.parseInt(BR.readLine());
Student S[] = new Student[n];
int i;
for (i = 0; i < n; i++)
{
S[i] = new Student();
S[i].ReadData(BR);
}
return S;
}
public static void main(String[] args)
{
Student Std[] = new Student[4];
Std[0] = new Student(101, "Mira ", 70.5);
Std[1] = new Student(102, "Masud ", 75.5);
Std[2] = new Student(103, "Monira ", 85.5);
Std[3] = new Student(104, "Monirul", 80.5);
int i;
try{
PrintWriter PR = new PrintWriter(new
FileWriter("c:\\student.txt"));
WriteData(Std, PR);
PR.close();
}
catch(IOException e)
{
System.out.print("Error: " + e);
System.exit(1);
}
try{
BufferedReader BR = new BufferedReader(new
FileReader("c:\\student.txt"));
Student S[] = ReadData(BR);
System.out.println( "Roll Name Mark");
System.out.println( "~~~~ ~~~~ ~~~~");
for (i = 0; i < S.length; i++)
{
S[i].display();
}
BR.close();
}
catch(IOException e)
{
System.out.print("Error: " + e);
System.exit(1);
}
}
}