66import java .io .IOException ;
77import java .io .PrintWriter ;
88import java .io .Writer ;
9+ import java .util .Arrays ;
10+ import java .util .List ;
911
1012import au .com .bytecode .opencsv .CSVWriter ;
1113
@@ -19,20 +21,20 @@ static int export(Database db, String tableName, Writer csv) throws IOException{
1921 CSVWriter writer = new CSVWriter (new BufferedWriter (csv ));
2022 int rows = 0 ;
2123 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 ++;
2631 }
27- writer .writeNext (buffer );
28- rows ++;
29- }
3032 }finally {
3133 writer .close ();
3234 }
3335 return rows ;
3436 }
35-
37+
3638 static void export (String filename , String tableName ) throws IOException {
3739 Database db = DatabaseBuilder .open (new File (filename ));
3840 try {
@@ -41,9 +43,9 @@ static void export(String filename, String tableName) throws IOException{
4143 db .close ();
4244 }
4345 }
44-
46+
4547 static void schema (String filename ) throws IOException {
46-
48+
4749 Database db = DatabaseBuilder .open (new File (filename ));
4850 try {
4951 for (String tableName : db .getTableNames ()){
@@ -58,9 +60,9 @@ static void schema(String filename) throws IOException{
5860 }finally {
5961 db .close ();
6062 }
61-
63+
6264 }
63-
65+
6466 static void exportAll (String filename ) throws IOException {
6567 Database db = DatabaseBuilder .open (new File (filename ));
6668 try {
@@ -79,21 +81,39 @@ static void exportAll(String filename) throws IOException{
7981 }catch (IOException ex ){}
8082 }
8183 }
82-
8384 }finally {
8485 db .close ();
85-
8686 }
87-
87+
8888 }
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+
90102 /**
91103 * @param args
92104 * @throws IOException
93105 */
94106 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+ }
97117 if (args .length == 1 ){
98118 exportAll (args [0 ]);
99119 System .exit (0 );
@@ -106,17 +126,8 @@ else if(args.length == 2){
106126 export (args [0 ], args [1 ]);
107127 System .exit (0 );
108128 }
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 ();
120131 System .exit (1 );
121132 }
122133
0 commit comments