-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParseFile.java
115 lines (99 loc) · 3.29 KB
/
ParseFile.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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package progress;
/**
*
* @author santosh
*/
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
public class ParseFile {
List<Employee> empList = new ArrayList();
String curCol = null;
public void parseFunction() {
try
{
String absolutePath = new File("").getAbsolutePath();
FileReader fin = new FileReader(absolutePath + "/src/progress/employees.txt");
Scanner fileRead = new Scanner(fin);
String line;
int count = 0;
Employee tmpEmp=null;
while(fileRead.hasNextLine())
{
line = fileRead.nextLine();
count = getSpaceCount(line);
StringTokenizer st = new StringTokenizer(line);
String str = null;
switch(count){
case 0:
System.out.println(st.nextToken());
break;
case 1:
str = st.nextToken();
if(str.equalsIgnoreCase(ColumnNames.COL1)){
tmpEmp = createEmployee();
curCol = ColumnNames.COL1;
}
else if (str.equalsIgnoreCase(ColumnNames.COL2))
curCol= ColumnNames.COL2;
else
curCol = ColumnNames.COL3;
break;
case 2:
str = st.nextToken();
if(curCol.equalsIgnoreCase(ColumnNames.COL1)){
tmpEmp.setId(str);
}
else if (curCol.equalsIgnoreCase(ColumnNames.COL2))
tmpEmp.setName(str);
else
{
tmpEmp.setSalary(str);
empList.add(tmpEmp);
}
break;
}
}
}
catch(IOException e)
{
System.err.println(e);
}
}
private int getSpaceCount(String line) {
char[] toChar = line.toCharArray();
int i=0;
while(toChar[i] != 0)
{
if(toChar[i] != ' ')
break;
else
i++;
}
return i;
}
public void printList() {
System.out.println("List is :");
for(Employee e : empList){
System.out.println("ID :" + e.getId());
System.out.println("Name :" + e.getName());
System.out.println("Salary :" + e.getSalary());
}
}
public List<Employee> getListEmp(){
return empList;
}
private Employee createEmployee() {
Employee tmp = new Employee();
return tmp;
}
}