6
6
import java .io .IOException ;
7
7
import java .io .PrintWriter ;
8
8
import java .io .Writer ;
9
+ import java .util .Arrays ;
10
+ import java .util .List ;
9
11
10
12
import au .com .bytecode .opencsv .CSVWriter ;
11
13
@@ -19,20 +21,20 @@ static int export(Database db, String tableName, Writer csv) throws IOException{
19
21
CSVWriter writer = new CSVWriter (new BufferedWriter (csv ));
20
22
int rows = 0 ;
21
23
try {
22
- for (Row row : table ){
23
- int i = 0 ;
24
- for (Object object : row .values ()) {
25
- buffer [i ++] = object == null ? null : object .toString ();
24
+ for (Row row : table ){
25
+ int i = 0 ;
26
+ for (Object object : row .values ()) {
27
+ buffer [i ++] = object == null ? null : object .toString ();
28
+ }
29
+ writer .writeNext (buffer );
30
+ rows ++;
26
31
}
27
- writer .writeNext (buffer );
28
- rows ++;
29
- }
30
32
}finally {
31
33
writer .close ();
32
34
}
33
35
return rows ;
34
36
}
35
-
37
+
36
38
static void export (String filename , String tableName ) throws IOException {
37
39
Database db = DatabaseBuilder .open (new File (filename ));
38
40
try {
@@ -41,9 +43,9 @@ static void export(String filename, String tableName) throws IOException{
41
43
db .close ();
42
44
}
43
45
}
44
-
46
+
45
47
static void schema (String filename ) throws IOException {
46
-
48
+
47
49
Database db = DatabaseBuilder .open (new File (filename ));
48
50
try {
49
51
for (String tableName : db .getTableNames ()){
@@ -58,9 +60,9 @@ static void schema(String filename) throws IOException{
58
60
}finally {
59
61
db .close ();
60
62
}
61
-
63
+
62
64
}
63
-
65
+
64
66
static void exportAll (String filename ) throws IOException {
65
67
Database db = DatabaseBuilder .open (new File (filename ));
66
68
try {
@@ -79,21 +81,39 @@ static void exportAll(String filename) throws IOException{
79
81
}catch (IOException ex ){}
80
82
}
81
83
}
82
-
83
84
}finally {
84
85
db .close ();
85
-
86
86
}
87
-
87
+
88
88
}
89
-
89
+
90
+ static void printUsage (){
91
+ System .out .println ("Usage:" );
92
+ System .out .println (" java -jar access2csv.jar [ACCESS FILE] [OPTIONS]" );
93
+ System .out .println ("" );
94
+ System .out .println ("Options:" );
95
+ System .out .println ("" );
96
+ System .out .println (" * if no options are provided, all tables will be exported to CSV files," );
97
+ System .out .println (" one file per table. Output file paths will be printed to stdout" );
98
+ System .out .println (" * '--schema' - prints the database schema" );
99
+ System .out .println (" * [TABLENAME] - prints the given table as CSV to stdout" );
100
+ }
101
+
90
102
/**
91
103
* @param args
92
104
* @throws IOException
93
105
*/
94
106
public static void main (String [] args ) throws IOException {
95
- // TODO Auto-generated method stub
96
-
107
+ List <String > helpCommands = Arrays .asList (new String []{"-h" , "--help" , "-H" , "/?" });
108
+
109
+ if (args .length == 1 && helpCommands .contains (args [0 ])){
110
+ printUsage ();
111
+ System .exit (0 );
112
+ }
113
+ if (args .length == 1 && args [0 ].equals ("--schema" )){
114
+ exportAll (args [0 ]);
115
+ System .exit (0 );
116
+ }
97
117
if (args .length == 1 ){
98
118
exportAll (args [0 ]);
99
119
System .exit (0 );
@@ -106,17 +126,8 @@ else if(args.length == 2){
106
126
export (args [0 ], args [1 ]);
107
127
System .exit (0 );
108
128
}
109
-
110
- System .out .println ("Invalid arguments. Usage:" );
111
- System .out .println (" java -jar access2csv.jar [ACCESS FILE] [OPTIONS]" );
112
- System .out .println ("" );
113
- System .out .println ("Options:" );
114
- System .out .println ("" );
115
- System .out .println (" * if no options are provided, all tables will be exported to CSV files," );
116
- System .out .println (" one file per table. Output file paths will be printed to stdout" );
117
- System .out .println (" * '--schema' - prints the database schema" );
118
- System .out .println (" * [TABLENAME] - prints the given table as CSV to stdout" );
119
-
129
+ System .err .println ("Invalid arguments." );
130
+ printUsage ();
120
131
System .exit (1 );
121
132
}
122
133
0 commit comments