C#: Appending *contents* of one text file to another text file -
there no other way this, there way append contents of 1 text file text file, while clearing first after move?
the way know use reader , writer, seems inefficient large files...
thanks!
no, don't think there's this.
if 2 files use same encoding , don't need verify they're valid, can treat them binary files, e.g.
using (stream input = file.openread("file1.txt")) using (stream output = new filestream("file2.txt", filemode.append, fileaccess.write, fileshare.none)) { input.copyto(output); // using .net 4 } file.delete("file1.txt");
note if file1.txt
contains byte order mark, should skip past first avoid having in middle of file2.txt
.
if you're not using .net 4 can write own equivalent of stream.copyto
... extension method make hand-over seamless:
public static class streamextensions { public static void copyto(this stream input, stream output) { if (input == null) { throw new argumentnullexception("input"); } if (output == null) { throw new argumentnullexception("output"); } byte[] buffer = new byte[8192]; int bytesread; while ((bytesread = input.read(buffer, 0, buffer.length)) > 0) { output.write(buffer, 0, bytesread); } } }
Comments
Post a Comment