I have written program to write big set of data from oracle database directly to text file using fileStream and streamWriter classes. However, I have to make sure size of the text file won't exceed 2 gb and if it exceeds, program should create another file and start writing the text on it and so on. I have to write some additional text to each 2b files after they are created, if process creates more than one file.
Somehow, I have managed to achieve the result but I am not satisfied with the code. My steps are
- create method to fetch data from the Db using DbDataReader and in reader.Read() while loop, call a delegate passing reader as parameter.
- In calling class/method, I called above method and pass a function (aka delegate) which read data from the dataReader and write directly through streamWriter.
- So, in each loop iteration, I am writing to the file. Although, I have put buffer size while create FileStream and StreamWriter to not to flush data in each loop iteration but I have written code in Main method/other methods to create and dispose the fileStream/StreamWriter.
=My Code looks similar like this
new MyDataReader().ReadFor("SOME_PARAM",
(dataReader) =>
{
StreamWriter writer = PrepareFileWriter();
writer.WriteLine(dataReader["Data"]);
},
(() => // another delegate to cleanUp (close streamWriter etc). called once all data is read.
{
WriteHeaderTrailerAndCloseFile();
}));
PrepareFileWriter and WriteHeaderTrailerAndCloseFile methods are responsible for creating and disposing StreamWriter instances which could be another methods in my calling class. PrepareFileWriter method also manage that if fileStream length increases 2b then create new StreamWriter and returns.
I am thinking about improving the code to manage Io operations (open/writie/close through streamWriter) better way which are scattered and unorganized.
Am I on right track? What are the known patterns/way of achieving such kind of functionality? Anyway, I am looking for more approach than code itself.
Aucun commentaire:
Enregistrer un commentaire