File stream operations in VB.net
By: Issac
The FileStream class allows us to move data to and from the stream as arrays of bytes, a filestream actually symbolizes a a file in a system. We operate File using FileMode in FileStream Class
Some of the FileModes are as Follows :
FileMode.Append: It opens and appends to a file, if the file does not exist, it creates a new file
FileMode.Create: Creates a new file, if the file already exists it will appends to it.
FileMode.CreateNew: Creates a new file, if the file exists it throws an exception.
FileMode.Open: Opens an existing file
How to create a file using VB.NET FileStream?
The following example shows, how to write in a file using FileStream.
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim wFile As System.IO.FileStream
Dim byteData() As Byte
byteData = Encoding.ASCII.GetBytes("FileStream Test1")
wFile = New FileStream("streamtest.txt", FileMode.Append)
wFile.Write(byteData, 0, byteData.Length)
wFile.Close()
Catch ex As IOException
MsgBox(ex.ToString)
End Try
End Sub
End Class
When we execute the program, it creates a new File and writes the content to it.
Archived Comments
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Using Resume Next and Resume Line in VB.net
Using On Error GoTo 0 in VB.net
Getting an Exception's Number and Description in VB.net
Raising an Exception Intentionally in VB.net
Exception Filtering in the Catch Block in VB.net
Using Multiple Catch Statements in VB.net
Throwing an Exception in VB.net
Throwing a Custom Exception in VB.net
Changes in Controls from VB6 to VB.net
Unstructured Exception Handling in VB.net