1 8 package org.jutils.csv; 9 10 import java.io.EOFException ; 11 import java.io.FileWriter ; 12 import java.io.IOException ; 13 import java.io.PrintWriter ; 14 import java.io.Writer ; 15 16 33 public class CSVWriter { 34 35 49 public CSVWriter(PrintWriter pw, boolean forceQuotes, char separator, String lineSeparator) { 50 this.pw = pw; 51 this.forceQuotes = forceQuotes; 52 this.separator = separator; 53 this.comment = "# "; 54 this.lineSeparator = lineSeparator; 55 } 57 public CSVWriter(Writer w, boolean forceQuotes, char separator, String lineSeparator) { 58 this(new PrintWriter (w),forceQuotes,separator,lineSeparator); 59 } 60 61 66 public CSVWriter(PrintWriter pw) { 67 this.pw = pw; 68 this.forceQuotes = false; 69 this.separator = ','; 70 this.comment = "# "; 71 this.lineSeparator = System.getProperty("line.separator"); 72 } 74 75 public CSVWriter(Writer w) { 76 this(new PrintWriter (w)); 77 } 78 79 85 public CSVWriter(PrintWriter pw, char comment) { 86 this.pw = pw; 87 this.forceQuotes = false; 88 this.separator = ','; 89 this.comment = String.valueOf(comment) + " "; 90 this.lineSeparator = System.getProperty("line.separator"); 91 } 93 public CSVWriter(Writer w, char comment) { 94 this(new PrintWriter (w),comment); 95 } 96 97 100 PrintWriter pw; 101 102 106 boolean forceQuotes; 107 108 112 char separator; 113 114 119 boolean wasPreviousField = false; 120 121 124 String comment; 125 126 129 String lineSeparator; 130 131 136 public void writeCommentln(String text) { 137 if (wasPreviousField) writeln(); pw.print(comment); 139 write(text); 141 writeln(); 142 } 144 148 public void writeln(String token) { 149 write(token); 150 writeln(); 151 } 153 156 public void writeln() { 157 158 wasPreviousField = false; 159 pw.print(lineSeparator); 160 } 162 166 public void writeln(String [] line) { 167 for(int ii=0; ii < line.length; ii++) { 168 write(line[ii]); 169 } 171 writeln(); 173 } 175 183 public void write(String s) { 184 if ( wasPreviousField ) { 185 pw.print(separator); 186 } 187 188 if ( s == null ) { 189 pw.print(""); 190 return; 191 } 193 s = s.trim(); 194 if ( s.indexOf('\"') >= 0 ) { 195 196 pw.print ('\"'); 197 for ( int i=0; i<s.length(); i++ ) { 198 char c = s.charAt(i); 199 if ( c == '\"' ) { 200 pw.print("\"\""); 201 } else { 202 pw.print(c); 203 } 204 } 205 pw.print ('\"'); 206 } else if ( s.indexOf('\n') >=0 ) { 208 pw.print ('\"'); 210 for ( int i=0; i<s.length(); i++ ) { 211 char c = s.charAt(i); 212 if ( c == '\n' ) { 213 pw.print("\\n"); 214 } else { 215 pw.print(c); 216 } 217 } 218 pw.print ('\"'); 219 } else if ( forceQuotes || s.indexOf(separator) >= 0 ) { 221 222 pw.print ('\"'); 223 pw.print(s); 224 pw.print ('\"'); 225 } else { 226 227 pw.print(s); 228 } 229 230 wasPreviousField = true; 231 } 233 236 public void close() { 237 if ( pw != null ) { 238 pw.close(); 239 pw = null; 240 } } 243 248 static public void main(String [] args) { 249 try { 250 PrintWriter pw = new PrintWriter ( new FileWriter (args[0])); 252 CSVWriter csv = new CSVWriter(pw, false, ',', System.getProperty("line.separator") ); 253 csv.writeCommentln("This is a test csv-file: '" + args[0] + "'"); 254 csv.write("abc"); 255 csv.write("def"); 256 csv.write("g h i"); 257 csv.write("jk,l"); 258 csv.write("m\"n\'o "); 259 csv.writeln(); 260 csv.write("m\"n\'o "); 261 csv.write(" "); 262 csv.write("a"); 263 csv.write("x,y,z"); 264 csv.write("x;y;z"); 265 csv.writeln(); 266 csv.writeln(new String [] {"This", "is", "an", "array."}); 267 csv.close(); 268 } catch ( IOException e ) { 269 e.printStackTrace(); 270 System.out.println(e.getMessage()); 271 } 272 } 274 } 276 | Popular Tags |