Created
April 12, 2013 12:58
-
-
Save eranbetzalel/5371817 to your computer and use it in GitHub Desktop.
Simple implementation of CSV (or any other character delimited file) writer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Car | |
{ | |
public int CarId { get; set; } | |
public DateTime ManufactureDate { get; set; } | |
public decimal SomeDecimal { get; set; } | |
} | |
class DelimitedFileWriterExample | |
{ | |
static void Main() | |
{ | |
using (var delimitedFileStream = | |
new DelimitedFileWriter<Car>( | |
"C:\\abc.csv", | |
arg => new[] | |
{ | |
arg.ManufactureDate.ToString(), | |
arg.CarId.ToString(), | |
arg.SomeDecimal.ToString() | |
}, | |
() => new[] | |
{ | |
"ManufactureDate", "CarId", "SomeDecimal" | |
})) | |
{ | |
delimitedFileStream.WriteObject( | |
new Car | |
{ | |
ManufactureDate = DateTime.UtcNow, | |
CarId = 5, | |
SomeDecimal = (decimal) 4.23890 | |
}); | |
} | |
} | |
} | |
public class DelimitedFileWriter<T> : IDisposable | |
{ | |
private readonly char _delimiter; | |
private readonly Func<T, string[]> _getObjectStrings; | |
private readonly Encoding _encoding; | |
private readonly FileStream _fileStream; | |
private readonly StringBuilder _objectStringBuilder; | |
public DelimitedFileWriter( | |
string path, Func<T, string[]> getObjectStrings, Func<string[]> getHeaderStrings = null, Encoding encoding = null) | |
: this(path, ',', getObjectStrings, getHeaderStrings, encoding) | |
{ | |
} | |
public DelimitedFileWriter( | |
string path, char delimiter, Func<T, string[]> getObjectStrings, Func<string[]> getHeaderStrings = null, Encoding encoding = null) | |
{ | |
_delimiter = delimiter; | |
_getObjectStrings = getObjectStrings; | |
_encoding = encoding ?? Encoding.Default; | |
_objectStringBuilder = new StringBuilder(); | |
_fileStream = new FileStream(path, FileMode.Append); | |
if (getHeaderStrings != null && _fileStream.Length == 0) | |
WriteObjectString(getHeaderStrings()); | |
} | |
public void WriteObject(T objToWrite) | |
{ | |
if (!_fileStream.CanWrite) | |
throw new Exception("Cannot write to file."); | |
WriteObjectString(_getObjectStrings(objToWrite)); | |
} | |
private void WriteObjectString(string[] objectStrings) | |
{ | |
_objectStringBuilder.Clear(); | |
for (var i = 0; i < objectStrings.Length; i++) | |
{ | |
_objectStringBuilder.Append(objectStrings[i]); | |
if (i < objectStrings.Length - 1) | |
{ | |
_objectStringBuilder.Append(_delimiter); | |
} | |
else | |
{ | |
_objectStringBuilder.Append("\r\n"); | |
} | |
} | |
var bytesToWrite = _encoding.GetBytes(_objectStringBuilder.ToString()); | |
_fileStream.Write(bytesToWrite, 0, bytesToWrite.Length); | |
_fileStream.Flush(); | |
} | |
public void Dispose() | |
{ | |
_fileStream.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment