I want to write a byte array
to the end of an existing file. How to append 开发者_开发技巧byte array to file?
Here is the solution. Just use the following sub and provide the parameters as required:
Parameters description:
FilepathToAppendTo is the filepath you need to append the byte array
Content is your byte array
Private Sub AppendByteToDisk(ByVal FilepathToAppendTo As String, ByRef Content() As Byte)
Dim s As New System.IO.FileStream(FilepathToAppendTo, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite)
s.Write(Content, 0, Content.Length)
s.Close()
End Sub
Use System.IO.FileStream
class methods. Open/create a FileStream in append file mode.
System.IO.FileStream(filename,System.IO.FileMode.Append)
Dim bufData As Byte()
' write the entire buffer in one line of code
My.Computer.FileSystem.WriteAllBytes("BinaryFile.DAT", bufData, append := True)
Assumptions.
- You want to use UTF8 encoding.
using( var stream = File.AppendText(@"D:\test.txt"))
{
stream.WriteLine(Encoding.UTF8.GetString( b ) );
}
VB Version:
Using stream = File.AppendText("D:\test.txt")
stream.WriteLine(Encoding.UTF8.GetString(b))
End Using
精彩评论