-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSVextract.java
132 lines (124 loc) · 3.25 KB
/
CSVextract.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;
import com.univocity.parsers.common.processor.BeanListProcessor;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
public class CSVextract {
public int row,column,datarow=row-1,datacolumn = column-1;
public String[][] entry;
public CSVextract(File file)
{
try {
readCSV(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String[][] getEntry()
{
return entry;
}
public double[][] getDataTable()
{
double dataTable[][] = new double[row][column];
for(int i=1;i<row;i++)
{
for(int j=1;j<column;j++)
{
dataTable[i][j] = Double.parseDouble(entry[i][j]);
}
}
return dataTable;
}
public String[] getFields()
{
String field[] = new String[column-1];
for(int i=1;i<row;i++)
{
field[i]= entry[0][i];
}
return field;
}
public String[] getKey()
{
String[] key = new String[row-1];
for(int i=1;i<row;i++)
{
key[i] = entry[i][0];
}
return key;
}
public String[][] readCSV (File file) throws FileNotFoundException
{
String entry[][] = new String[100][100];
row=0;column=0;int columnCount=0;
/*Scanner reader;
try
{
reader = new Scanner(file);
Scanner sc2 = new Scanner(file);
while (sc2.hasNextLine())
{
for (String item : sc2.next().split(","))
{
entry[row][column]=reader.next();
column++;
System.out.println(item);System.out.println(column+" "+row);
}
}*/
/////
/*while(reader.hasNext())
{
reader.useDelimiter(",|\\r\\n");
while(reader.hasNext())
{
try{
if(Pattern.matches(Pattern.compile("\\r\\n"),reader.next()))
{
row++;System.out.println("br");
}
System.out.println(column+" "+row);
entry[row][column]=reader.next();
column++;
}catch(NumberFormatException e) {}
}
}
column ++;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
{
//System.out.print(entry[i][j]+" ");
}
System.out.println();
}*/
// 1st, config the CSV reader
CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setLineSeparator("\n");
settings.selectFields("Color1", "Color3", "Color2");
// 2nd, creates a CSV parser with the configs
CsvParser parser = new CsvParser(settings);
// 3rd, parses all rows of data in selected columns from the CSV file into a matrix
List<String[]> resolvedData = parser.parseAll(new FileReader(file));
// 3rd, process the matrix with business logic
for (String[] row : resolvedData)
{
StringBuilder strBuilder = new StringBuilder();
for (String col : row)
{
strBuilder.append(col).append("\t");
}
System.out.println(strBuilder);
}
return entry;
}
}