Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ************************************************************************
  #
  #  DivConq
  #
  #  http://divconq.com/
  #
  #  Copyright:
  #    Copyright 2014 eTimeline, LLC. All rights reserved.
  #
 #  License:
 #    See the license.txt file in the project's top-level directory for details.
 #
 #  Authors:
 #    * Andy White
 #
 ************************************************************************ */

Author(s):
Thomas Davis (sunsetbrew)
Copyright:
Copyright (c) 2010, Thomas Davis
License:
http://opensource.org/licenses/mit-license.php MIT License
 
 package divconq.util;
 
 import java.io.Writer;
 
CSV4180Writer provides a simple way to export CSV values to a file or console. The writer extends BufferedWriter for improved performance.
 
 public class CSVWriter extends BufferedWriter {
 	static public OperationResult writeRecords(OutputStream outListStruct recordsString... fields) {
 		
 		try {
 			CSVWriter wrt = new CSVWriter(new OutputStreamWriter(out));
 			
 			for (String fld : fields) {
 				wrt.writeField(fld);
 			}
 			
 			wrt.newLine();
 			
 			for (Struct s : records.getItems()) {
 				RecordStruct rec = (RecordStructs;
 
 				for (String fld : fields
 					wrt.writeField(Struct.objectToString(rec.getField(fld)));
 				
 				wrt.newLine();
 			};
 			
 			wrt.close();
 		}
 		catch (Exception x) {
 			or.error("Unable to write CSV: " + x);
 		}
 		
 		return or;
 	}

Create a buffered character-output stream that uses a default-sized output buffer.

Parameters:
out A writer
 
 	public CSVWriter(Writer out) {
 		super(out);
 	}

Create a new buffered character-output stream that uses an output buffer of the given size.

Parameters:
out A writer
sz Output-buffer size, a positive integer
 
 	public CSVWriter(Writer outint sz) {
 		super(outsz);
 	}

Writes a line of fields to the writer. A new line is automatically added if beyond the first call to this method. This is a convenience method.

Parameters:
fields fields to output
Throws:
java.io.IOException on general I/O error
	public void writeFields(ArrayList<Stringfieldsthrows IOException {
		if () {
			this. = false;
else {
			this.newLine();
		}
		Iterator<Stringsi = fields.iterator();
		while (si.hasNext()) {
			this.writeField(si.next());
		}
	}

Writes a line of fields to the writer. A new line is automatically added if beyond the first call to this method. This is a convenience method.

Parameters:
fields fields to output
Throws:
java.io.IOException on general I/O error
	public void writeFields(String[] fieldsthrows IOException {
		if () {
			this. = false;
else {
			this.newLine();
		}
		for (int i = 0; i < fields.lengthi++) {
			this.writeField(fields[i]);
		}
	}

Write a line separator.

Throws:
java.io.IOException If an I/O error occurs
See also:
java.io.BufferedWriter.newLine()
	public void newLine() throws IOException {
		this. = true;
		super.write("\r\n");
	}

Write a field to the output quoting as necessary and adding comma separators between fields.

Parameters:
field String to write
Throws:
java.io.IOException If an I/O error occurs
	public void writeField(String fieldthrows IOException {
		if (this.) {
			this. = false;
else {
			this.write(',');
		}
		// case 0: empty string, simple :)
		if ((field == null) || (field.length() == 0)) {
			return;
		}
		// case 1: field has quotes in it, if so convert to, quote field and
		// double all quotes
		Matcher matcher = .matcher(field);
		if (matcher.find()) {
			this.write('"');
			do {
				matcher.appendReplacement(this."\"\"");
while (matcher.find());
			matcher.appendTail(this.);
			this.write(this..toString());
			this.write('"');
			return;
		}
		// case 2: field has a comma, carriage return or new line in it, if so
		// quote field and double all quotes
		matcher = .matcher(field);
		if (matcher.find()) {
			this.write('"');
			this.write(field);
			this.write('"');
			return;
		}
		// case 3: safe string to just add
		this.append(field);
	}

Ignore:
	private boolean newLine = true;

Ignore:
	private final StringBuffer tmpBuffer = new StringBuffer();

Ignore:
	private static Pattern escapePattern = Pattern.compile("(\")");

Ignore:
	private static Pattern specialCharsPattern = Pattern.compile("[,\r\n]");

Ignore:
	private boolean newWriter = true;
New to GrepCode? Check out our FAQ X