Is2215 lecture6 lecturer_file_access

Preview:

DESCRIPTION

 

Citation preview

FILE ACCESSSequential Files

System.IO

File Handling in VB.NET is based on the System.IO Namespace

It contains a class library that allows for string, character and file manipulation

These classes allow us to: Read Write Copy Delete Manipulate Files

VB .NET IO Namespace

Commonly Used IO Classes

File Stream Class: provides access to standard input and

output files StreamReader and StreamWriter Class:

classes enables us to read or write a sequential stream of characters to or from a file

BinaryReader and BinaryWriter Class The BinaryReader and BinaryWriter classes

enable us to read and write binary data, raw 0's and 1's

Creating a Sequential File

Use the StreamWriter Class Choose a Filename (256 characters max) Choose a file path for the folder to contain the file Execute the a statement in the form of:

Dim sw as IO.StreamWriter = _ IO.File.CreateText(filespec)

Place data into the file using the WriteLine Method

sw.WriteLine(“text”) After data has been placed close the

StreamWriter sw.close

Reading a Sequential File

Use the StreamReader class to open the file for input

Select the File to Use Read from the file using the

ReadLine Method

Adding to a Sequential File

Use the StreamWrite to open the file for appending

Dim sw As IO.StreamWriter = _ IO.File.AppendText(filespec)

Place data into the File Using WriteLine method

Once you have finished writing to the file close the StreamWriter

StreamWriter/Reader with Search Functionality

Another Example

Other File Mg’t Operations

Delete a FileIO.File.Delete(filespec)

MoveIO.File.Delete(filespec,

newfilespec) You can simplify code by using

Imports System.IO