Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Take Advantage of Streams and Formatters in VB.NET  : Page 2

.NET File I/O operations use Stream and Formatter classes that abstract many tasks you had to code manually in classic VB.


advertisement
To work with a local disk file, you use the FileStream class, which lets you move data to and from the stream as arrays of bytes. To make it easier to read and write basic data types, you can use the methods of the BinaryReader and BinaryWriter classes, or the equivalent methods of the StreamReader and StreamWriter classes. All these classes wrap an underlying FileStream and provide methods that make it easier to read and write data in the appropriate format. The BinaryReader/Writer classes use the native form of the basic data types and produce binary files that are not readable by humans. The StreamReader/Writer classes convert basic data types into XML format and produce text files. All the classes work with any type of data, so the distinction between text and binary files is no longer as important as it used to be in classic VB. You can store numbers either as text (in XML format), or in their native format.

VB.NET supports traditional random access files, but it doesn't really need them. You can still create files that store structures, and access them by record numbers, as you did with previous versions of Visual Basic using the FileOpen and FileGet functions, but for the most part, the functionality of random access files has been replaced by XML and/or databases. If you are designing new applications and don't need compatible random access capability you should use the newer .NET capabilities.

No matter which class you decide to use to access a file, you must first create a FileStream object. There are several ways to do that. The simplest method is to specify the file and how it will be opened in the FileStream object's constructor, which has the following syntax:

Dim fStream As New FileStream(path, _ fileMode, fileAccess)

The path argument contains the full pathname of the file you want to open. The fileMode argument is a member of the FileMode enumeration (see Table 1) that determines how to open (or create) the specified file. The fileAccess argument is a member of the FileAccess enumeration: Read (for reading only), ReadWrite (for reading and writing), and Write (for writing only).determines the read/write access to the file.

Table 1: The Members of the FileMode Enumeration

Append



Opens an existing file and moves to the end of it, or creates a new file. Use this mode when the file is opened for writing

Create

Creates a new file if the specified file exists, or overwrites the existing file.

CreateNew

Creates a new file.  If the path argument specifies an existing file, an exception will be thrown.

Open

Opens an existing file. If the path argument specifies a file that doesn't exist, an exception will be thrown.

OpenOrCreate

Opens the specified file if it exists, or creates a new one.

Truncate

Opens the specified  file and resets its size to 0 bytes.


Creating a FileStream object is not the only way to open a file. You can also use one of the various Open methods of the File object (Open, OpenRead, OpenText, OpenWrite). These methods accept the file's path as argument and return a Stream object:

Dim FS As New FileStream = IO.File.OpenWrite("c:\Stream.txt")

Another way to open a file is to use the OpenFile method of the OpenFileDialog and SaveFileDialog controls. With the OpenFile method of these two controls you need not specify any arguments; both methods open the file selected by the user in the dialog. The OpenFile method of the OpenFileDialog control opens the file in read-only mode, whereas the OpenFile method of the SaveFileDialog control opens the file in read/write mode.

The FileStream class supports only the most basic file operation—moving data into or out of files as bytes or arrays of bytes. To use a FileStream instance to write something to a file, you must first convert the data to an array of bytes and then pass it as argument to the FileStream object's Write method. Likewise, the FileStream object's Read method returns an array of bytes. You must also specify how many bytes should be read from the file. You probably will not use the FileStream class methods directly often, but it's worth exploring briefly to see the base capabilities.

After creating a FileStream object, you can call its WriteByte to write a single byte or its Write method to write an array of bytes to the file. The WriteByte method accepts a byte as argument and writes it to the file, and the Write method accepts three arguments: an array of bytes, an offset in the array and the number of bytes to be written to the file. The syntax of the Stream.Write method is:

Write(buffer, offset, count)

The buffer argument is the array containing the bytes to be written to the file, offset is the index of the first byte you want to write from the array, and count is the number of bytes to write. The syntax of the Read method is identical, except that the Read method fills the array buffer with count characters from the file.

Converting even basic data types to bytes is not trivial and you should usually avoid using FileStreams directly; however, if you do plan to use the Stream object to write to a file, you should investigate the GetBytes and GetChars methods of the ASCIIEncoding and UnicodeEncoding classes (part of the System.Text namespace). For example, you can convert a string to an array of bytes with the following code:

Dim buffer() As Byte Dim encoder As New System.Text.ASCIIEncoding() Dim str As String = "This is a line of text" ReDim buffer(str.Length - 1) encoder.GetBytes(str, 0, str.Length, buffer, 0) FS.Write(buffer, 0, buffer.Length)

Notice that you must resize the buffer array to the length of the string you want to convert. To convert an array of bytes returned by the FileStream.Read method, use the GetChars method of the encoder variable.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap