1 package net.sf.pzfilereader.examples.createsamplecsv;
2
3 import java.io.FileWriter;
4 import java.io.PrintWriter;
5
6
7
8
9
10
11 /**
12 * @author zepernick
13 *
14 * Creates a sample csv file with the specified number of columns and rows
15 */
16 public class CSVTestFileCreator {
17
18 public static void main(final String[] args) {
19 int cols = 0;
20 int rows = 0;
21
22 if (args.length != 2) {
23 printUsage();
24 return;
25 }
26
27 try {
28 cols = Integer.parseInt(args[0]);
29 rows = Integer.parseInt(args[1]);
30 } catch (final Exception ex) {
31 printUsage();
32 return;
33 }
34
35 createFile(cols, rows);
36 }
37
38 public static void createFile(int cols, int rows) {
39 createFile(cols,rows,"SampleCSV.csv");
40 }
41
42 public static void createFile(int cols, int rows, final String filename) {
43 FileWriter fw = null;
44 PrintWriter out = null;
45 try {
46
47 fw = new FileWriter(filename);
48 out = new PrintWriter(fw);
49
50
51 for (int i = 1; i <= cols; i++) {
52 if (i > 1) {
53 out.write(",");
54 }
55 out.write("\"column " + i + "\"");
56 }
57 out.write("\r\n");
58 out.flush();
59
60
61 for (int i = 1; i <= rows; i++) {
62 for (int j = 1; j <= cols; j++) {
63 if (j > 1) {
64 out.write(",");
65 }
66 out.write("\"data " + j + "\"");
67 }
68
69 out.write("\r\n");
70 out.flush();
71 if (i % 100000 == 0) {
72 System.out.print(".");
73 }
74 }
75
76 } catch (final Exception ex) {
77 ex.printStackTrace();
78 } finally {
79 try {
80 if (out != null) {
81 out.close();
82 }
83 if (fw != null) {
84 fw.close();
85 }
86 } catch (final Exception ignore) {
87 }
88
89 }
90 }
91
92 private static void printUsage() {
93 System.out.println("INVALID USAGE...");
94 System.out.println("PARAMETER 1 = # OF COLUMNS");
95 System.out.println("PARAMETER 2 = # OF ROWS");
96 System.out.println("Example - java CSVTestFileCreator 10 100");
97 }
98 }