forked from cherkavi/java-code-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.java
More file actions
executable file
·54 lines (47 loc) · 1.49 KB
/
Example.java
File metadata and controls
executable file
·54 lines (47 loc) · 1.49 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
import com.csvreader.*;
public class Example {
public static void main(String[] args){
String path_to_file="c:\\temp.csv";
write(path_to_file);
read(path_to_file);
}
private static void write(String path_to_file){
try{
com.csvreader.CsvWriter writer=new com.csvreader.CsvWriter(path_to_file);
writer.writeRecord(new String[]{"hello","#from","file\"CSV\""});
writer.writeComment("hello");
writer.writeRecord(new String[]{"hello2","from2","file2\"CSV\""});
writer.flush();
writer.close();
}catch(Exception ex){
System.out.println("Error write:"+ex.getMessage());
}
}
private static void read(String path_to_file){
System.out.println("READ");
try{
System.out.println("begin");
com.csvreader.CsvReader reader=new com.csvreader.CsvReader(path_to_file);
/* System.out.println("read header");
reader.readHeaders();
for(int counter=0;counter<reader.getHeaderCount();counter++){
System.out.println(counter+":"+reader.getHeader(counter));
}
*/
System.out.println("read data");
reader.setComment('#');
reader.setUseComments(true);
while(reader.readRecord()){
for(int counter=0;counter<reader.getColumnCount();counter++){
System.out.print(reader.get(counter));
System.out.print(" -|- ");
}
System.out.println("");
}
reader.close();
System.out.println("end");
}catch(Exception ex){
System.out.println("Error in read CSV:"+ex.getMessage());
}
}
}