-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathMake records of students.java
81 lines (81 loc) · 2.62 KB
/
Make records of students.java
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
import java.io.*;
import java.util.*;
class TextFile
{
static void createTextFile(int n)
{
try //to handle exceptions
{
//to open TEXT file in output mode
FileWriter fw = new FileWriter("Stud.txt");
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
Scanner sc=new Scanner(System.in);
String name; //variable declaration
int rno; //variable declaration for roll number
double mks; //variable declaration for marks
//accepting data and writing in file
for(int i=1; i<=n; i++)
{ //input information from the user
System.out.println("Enter name "); name=sc.next();
System.out.println("Enter Roll No."); rno=sc.nextInt();
System.out.println("Enter Marks "); mks=sc.nextDouble();
//write information in the text file
pw.println(rno); //writing roll number in file
pw.println(name); //writing name in file
pw.println(mks); //writing marks in file
}
pw.close(); //closing the file otherwise data will not be saved
}
catch(Exception E) { } //handling of exception i.e. runtime errors
}
static void appendTextFile(int n)throws Exception
{ //to open TEXT file in Append mode
//so that more records can be added in an existing file
FileWriter fw = new FileWriter("Stud.txt",true);//Notice 'true'
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
String name; //variable declaration
int rno; //variable declaration for roll number
double mks; //variable declaration for marks
//accepting data and writing in file
for(int i=1; i<=n; i++)
{ //input information from the user
System.out.println("Enter name "); name=sc.next();
System.out.println("Enter Roll No."); rno=sc.nextInt();
System.out.println("Enter Marks "); mks=sc.nextDouble();
//write information in the text file
pw.println(rno); //writing roll number in file
pw.println(name); //writing name in file
pw.println(mks); //writing marks in file
}
pw.close(); //closing the file otherwise data will not be saved
}
static void readTextFile()throws Exception
{
//to open the existing data file in input mode
FileReader fr = new FileReader("stud.txt");
BufferedReader br = new BufferedReader(fr);
double sum=0;
int c=0;
try //handling exception
{
while(true)
{ //reading data from file
int rno=Integer.parseInt(br.readLine());
String name=br.readLine();
double num=Double.parseDouble(br.readLine());
sum=sum+num; //sum of all marks
c++; //counting number of records
//printing on screen
System.out.println(rno+"\t"+name+"\t"+num);
}
}
catch(Exception E) //handling of exception i.e. runtime errors
{
System.out.println(“End Of File Reached…..”);
}
fr.close(); //closing of file otherwise next time we cannot open it
System.out.println("Class Average "+sum/c);
}
}//end of class