/
UGradRecord.java
73 lines (70 loc) · 1.69 KB
/
UGradRecord.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
import java.util.*;
import java.io.*;
public class UGradRecord {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public static final String DELIM = "\t";
public static void recordToFile(Ugrad[] uGrads, String fileName)
{
try
{
PrintWriter fileWriter = new PrintWriter(
new FileOutputStream(fileName));
//Write your header
fileWriter.println("Num Students "+DELIM+uGrads.length);
//Write your body
for(int i = 0; i < uGrads.length; i++)
{
fileWriter.println(uGrads[i].getName() + DELIM +
uGrads[i].getID()+DELIM+
uGrads[i].getLevel());
}
fileWriter.close(); //DONT FORGET THIS
}
catch(Exception e)
{
System.out.println(e);
}
}
public static Ugrad[] readFromFile(String fileName)
{
try
{
Scanner fileScanner = new Scanner(new File(fileName));
//Reading the header
String fileLine = fileScanner.nextLine();
String[] splitLines = fileLine.split(DELIM);
if(splitLines.length != 2)
{
return null;
}
int uCount = 0;
Ugrad[] retU = new Ugrad[Integer.parseInt(splitLines[1])];
while(fileScanner.hasNextLine())
{
//Read it
fileLine = fileScanner.nextLine();
//Split it
splitLines = fileLine.split(DELIM);
//Check it
if(splitLines.length == 3)
{
//Apply it
String name = splitLines[0];
int id = Integer.parseInt(splitLines[1]);
int level = Integer.parseInt(splitLines[2]);
retU[uCount] = new Ugrad(name, id, level);
uCount++;
}
}
fileScanner.close();
return retU;
}
catch(Exception e)
{
System.out.println(e);
return null;
}
}
}