Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package base.text;
 
Output rows of records to an output stream in CSV format, as defined in: http://en.wikipedia.org/wiki/Comma-separated_values
public class CsvOutputStream implements Closeable {
	private OutputStream out;
	private CsvOutputStream() {
	}
		this. = out;
	}

Output a CSV formatted row to the output stream.

Parameters:
items
Throws:
java.io.IOException
	public void write(String[] itemsthrows IOException {
		boolean first = true;
		for(String item : items) {
			if(first) {
				first = false;
else {
				.write(',');
			}
			boolean hasQuote = item.indexOf('"') >= 0;
			if(item.indexOf(',') >= 0 || hasQuote || item.indexOf('\n') >= 0 || item.indexOf('\r') >= 0) {
				.write('"');
				if(hasQuote) {
					.write(item.replaceAll("\"""\"\"").getBytes("UTF-8"));
else {
					.write(item.getBytes("UTF-8"));
				}
				.write('"');
else {
				.write(item.getBytes("UTF-8"));
			}
		}
		.write('\n');
	}
	public void close() throws IOException {
	}
New to GrepCode? Check out our FAQ X