-
Notifications
You must be signed in to change notification settings - Fork 1
/
CSVFile.java
179 lines (141 loc) · 3.99 KB
/
CSVFile.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package creek;
import java.io.*;
import java.util.*;
import java.nio.file.*;
import java.nio.charset.*;
public class CSVFile implements TableFile {
private File file;
private CSV csv;
private static String removeBOM ( String raw ) {
// remove Byte Order Mark (BOM)
if (
raw != null &&
raw.length() > 3 &&
raw.charAt(0) == 0xEF &&
raw.charAt(1) == 0xBB &&
raw.charAt(2) == 0xBF
) return raw.substring(3);
return raw;
}
// constructors
public CSVFile ( String path ) throws Exception {
this( new File(path), true, null, "," );
}
public CSVFile ( String path, boolean append ) throws Exception {
this( new File(path), append, null, "," );
}
public CSVFile ( String path, boolean append, Table table ) throws Exception {
this( new File(path), append, table, "," );
}
public CSVFile ( File file ) throws Exception {
this( file, true, null, "," );
}
public CSVFile ( File file, boolean append ) throws Exception {
this( file, append, null, "," );
}
public CSVFile ( File file, boolean append, Table table, String comma ) throws Exception {
this.file = file;
csv = new CSV( comma, "\\", "\"" ); // create blank CSV object
if (append) {
read();
if (table!=null) append( new CSV( table ) ); // append table data to both file and CSV
} else {
write( table );
}
}
// TableFile interface
public TableFile create ( File file ) throws Exception {
return new CSVFile( file, false, null, "," );
}
public File file () {
return file;
}
public Table table () {
return csv;
}
public TableFile clear () throws Exception {
if (file.exists()) file.delete();
file.createNewFile(); // empty file
csv = new CSV( csv.comma(), csv.escape(), csv.quote() );
return this;
}
public TableFile read () throws Exception {
if (file.exists()) {
csv = new CSV (
removeBOM(
new String(
Files.readAllBytes( file.toPath() ),
Charset.defaultCharset()
)
), csv.comma()
);
} else {
clear();
}
return this;
}
public TableFile append ( Table table ) throws Exception {
return write( table, true );
}
public TableFile write ( Table table ) throws Exception {
return write( table, false );
}
public TableFile write () throws Exception {
return write( csv, false );
}
public TableFile write ( Table table, boolean append ) throws Exception {
if (! append) clear(); // write operation
if (table == null) return this;
CSV newCsv = new CSV( csv.comma(), csv.escape(), csv.quote() );
newCsv.append( table );
if (!file.exists()) file.createNewFile();
Files.write(
file.toPath(),
newCsv.serial().getBytes(),
( append ? StandardOpenOption.APPEND : StandardOpenOption.WRITE )
);
if (append) csv.append( newCsv ); // append operation
else csv = newCsv; // write operation
//System.out.println( "CSVFile: "+csv.data() );
return this;
}
public String toString () {
return csv.toString();
}
public static void main ( String[] args ) {
try {
CSVFile f0 = new CSVFile( args[0], false );
System.out.println( "********\nf0 empty:\n"+f0 );
f0.append( SimpleTable.test() );
System.out.println( "********\nf0 with test() data:\n"+f0 );
f0.write(
new CSV(
"1,2,3\n"+
"A,B,C\n"
)
);
System.out.println( "********\nf0 overwritten:\n"+f0 );
Thread.sleep(500);
CSVFile f1 = new CSVFile( args[0]+"_2.csv", true );
System.out.println( "********\nf1 as-is:\n"+f1 );
f1.append( f0.table() );
System.out.println( "********\nf1 with f0 appended:\n"+f1 );
Thread.sleep(1000);
f1.append(
new CSV(
"a,b,c\n"+
"d,e,f\n"
)
);
System.out.println( "********\nf1 with more data...\n"+f1 );
Thread.sleep(500);
CSVFile f2 = new CSVFile( args[0]+"_2.csv", true, new CSV( "|", "\\", "'" ) );
System.out.println( "********\nf2 read from f1 file...\n"+f2.table() );
(new CSVFile( args[0]+"_3.csv" ))
.append( f2.table() )
.append( f0.table() );
} catch (Exception e) {
e.printStackTrace();
}
}
}