Você está na página 1de 12

Getting Started With File Handling

File Handling
File handling in Visual Basic is based on System.IO namespace with a class library that supports string, character and file manipulation. These classes contain properties, methods and events for creating, copying, moving, and deleting files. Since both strings and numeric data types are supported, they also allow us to incorporate data types in files. The most commonly used classes are FileStream, BinaryReader, BinaryWriter, StreamReader and StreamWriter.

FileStream class
This class provides access to standard input and output files. We use the members of FileAccess, FileMode and FileShare Enumerations with the constructors of this class to create or open a file. After a file is opened it's FileStream object can be passed to the Binary Reader, BinaryWriter, Streamreader and StreamWriter classes to work with the data in the file.
Dim fs as new FileStream(<path of File>, FileMode, FileAccess, FileShare)

FileMode Enumeration
Members Append Create CreateNew Description Opens the file if it exists and seeks to the end of the file, or creates a new file. FileMode.Append can only be used in conjunction with FileAccess.Write Specifies that the operating system should create a new file. If the file already exists, it will be overwritten. Specifies that the operating system should create a new file. This requires FileIOPermissionAccess.Write. If the file already exists, an IOException is thrown. Specifies that the operating system should open an existing file. The ability to open the file is dependent on the the value specified by FileAccess. A System.IO.FileNotFoundException is thrown if the file does not exist. Specifies that the operating system should open a file if it exists; otherwise, a new file should be created. Specifies that the operating system should open an existing file. Once opened, the file should be truncated so that its size is zero bytes.

Open

OpenOrCreate Truncate

File Access Enumeration


Member Read

Description
Read access to the file. Data can be read from the file. Combine with Write for read/write access. Read and write access to the file. Data can be written to and read from the file. Write access to the file. Data can be written to the file. Combine with Read for read/write access.

ReadWrite Write

FileShare Enumerations
Members None
Description Description It is used to decline sharing of the current file. When this constant is used with the FileShare enumeration, a request to open the file will fail until the file is closed. It is used to allow subsequent opening of a file for reading. If this flag is not used, a request to open the file for reading will fail until the file is closed. It is used to allow subsequent opening of a file for reading or writing. If this flag is not used, any request to open the file for writing or reading will fail until the file is closed. It is used to allow subsequent opening of a file for writing. If this flag is not used, any request to open the file for writing will fail until the file is closed.

Read

ReadWrite

Write

BinaryReader and BinaryWriter Classes


Are used to read from and write to a binary file.

StreamReader and StreamWriter Classes


Are used to read and write data as streams of characters. Use specific encoding to convert characters to and from bytes.

StreamWriter Class
Method Name WriteLine Write Description

Writes some data as specified by the overloaded parameters, followed by a line terminator. Writes to the stream Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

Flush

StreamReader Class
Method Name Peek Read Description

Returns the next available character but does not consume it. Reads the next character or next set of characters from the input string. Reads a line from the underlying string. Reads the stream as a string, either in its entirety or from the current position to the end of the stream.

ReadLine

ReadToEnd

BinaryWriter Class
Method Name Description

Seek
Write

Sets the position within the current stream .


Writes to the stream Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

Flush

BinaryReader Class
Methods Read ReadBoolean ReadByte ReadDecimal ReadDouble ReadInt16

ReadBytes
ReadChar ReadChars ReadString

ReadInt32
ReadInt64 ReadSByte ReadSingle

DirectoryInfo Class
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.
Methods Name
Create

Description
Creates a directory. Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class. Deletes a DirectoryInfo and its contents from a path. Returns the subdirectories of the current directory. Returns a file list from the current directory.

CreateSubdirectory

Delete GetDirectories GetFiles

Você também pode gostar