-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeFileOp.java
More file actions
152 lines (115 loc) · 4.16 KB
/
Copy pathEmployeeFileOp.java
File metadata and controls
152 lines (115 loc) · 4.16 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//Rylee Davis
//cs272
//Lab1 EmployeFileOp.java
//August 28, 2019
package myprograms;
import java.io.*;
class EmployeeInformation
{
String name;
int number;
String state;
int zip;
int age;
String sex;
//ageGetter or isLessThan30 function
//Get CSVString
}
public class EmployeeFileOp
{
private static EmployeeInformation[] numEmployees = null;
// public int getAge()
// {
// return age;
// }//end getAge()
public static void read(String fname) //function read
{
int no = 0;
String line = "";
try
{
// FileReader reads text files in the default encoding.
FileReader fileReader = new FileReader(fname);
// Always wrap FileReader in BufferedReader.
BufferedReader bufferedReader = new BufferedReader(fileReader);
String input;
int counter = 0;
while((input = bufferedReader.readLine()) != null)
{
counter++;
}//end while
System.out.println("Count: " + counter);
while((line = bufferedReader.readLine()) != null) //will read until end ?
{
//parse line
if(no==0)
{
int totalNumber = Integer.parseInt(line);
numEmployees = new EmployeeInformation[totalNumber];
}//end if
else
{
String[] linestr = line.split(","); //use comma as separator
if(linestr.length!= 6) continue;
if(no > numEmployees.length)
{
System.out.println("There are too many employees in the file. ");
break;
}//end if
numEmployees[no-1] = new EmployeeInformation();
numEmployees[no-1].name = linestr[0];
numEmployees[no-1].number = Integer.parseInt(linestr[1]);
numEmployees[no-1].state = linestr[2];
numEmployees[no-1].zip = Integer.parseInt(linestr[3]);
numEmployees[no-1].age = Integer.parseInt(linestr[4]);
numEmployees[no-1].sex = linestr[5];
}//end else
}//end while
no++;
bufferedReader.close(); // Always close files.
}//end try
catch(FileNotFoundException ex)
{
System.out.println("Unable to open file '" + fname + "'");
}//end catch
catch(IOException ex)
{
System.out.println("Error reading file '" + fname + "'");
}//end catch
System.out.println("Finish reading pairs from file "+ fname);
}//end read
public static void write(String fname)
{
// if( <= 30)
try
{
File file = new File(fname);
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(numEmployees.length + "\n");
for(int i = 0;i < numEmployees.length;i++)
{
//System.out.println("i = " + i + ":" + numEmployees[i].name+ "," + numEmployees[i].number...);
bw.write(numEmployees[i].name + "," + numEmployees[i].number + "," + numEmployees[i].state + "," + numEmployees[i].zip + "," + numEmployees[i].age + "," + numEmployees[i].sex + "\n" );
}//end for
//close the writers
if(bw != null)
bw.close();
if(fw != null)
fw.close();
// bw.close();
// fw.close();
}//end try
catch (IOException e)
{
e.printStackTrace();
}//end catch
System.out.println("Finish writing pairs to file "+ fname);
}//end write
public static void main(String[] args)
{
numEmployees = null; //clear the memory
read("core_dataset.csv"); //read back the 100 pairs
write("young_employee.csv"); //write the pairs to another file
}//end main
}//end class