Вы находитесь на странице: 1из 14

FILE HANDLING

FILE HANDLING

Afileis a collection of data stored in a disk with a


specific name and a directory path.
When a file is opened for reading or writing, it
becomes astream.
The stream is basically the sequence of bytes
passing through the communication path.
There are two main streams: theinput streamand
theoutput stream.
Theinput streamis used for reading data from file
(read operation) and theoutput streamis used for
writing into the file (write operation).

I/O Class

Description

BinaryReader

Reads primitive data from a binary stream.

BinaryWriter

Writes primitive data in binary format.

BufferedStream

A temporary storage for a stream of bytes.

Directory

Helps in manipulating a directory structure.

DirectoryInfo

Used for performing operations on


directories.

DriveInfo

Provides information for the drives.

File

Helps in manipulating files.

I/O Class

Description

FileInfo

Used for performing operations on files.

FileStream
MemoryStream
Path
StreamReader

Used to read from and write to any location in


a file.
Used for random access to streamed data
stored in memory.
Performs operations on path information.
Used for reading characters from a byte
stream.

StreamWriter

Is used for writing characters to a stream.

StringReader

Is used for reading from a string buffer.

StringWriter

Is used for writing into a string buffer.

THE FILESTREAM CLASS

TheFileStreamclass in the System.IO namespace


helps in reading from, writing to and closing files.
This class derives from the abstract class Stream.

You need to create aFileStreamobject to create a


new file or open an existing file.

The syntax for creating aFileStreamobject is as


follows:

Dim <object_name> As FileStream = New


FileStream(<file_name>, <FileMode Enumerator>,
<FileAccess Enumerator>, <FileShare Enumerator>)

For example, for creating a FileStream objectFfor reading a


file namedsample.txt:

Dim f1 As FileStream = New


FileStream("test.dat", FileMode.OpenOrCreate,
FileAccess.ReadWrite)

PARAMETERS

FileMode:-TheFileModeenumerator defines various methods for opening files.


The members of the FileMode enumerator are:

Append: It opens an existing file and puts cursor at the end of


file, or creates the file, if the file does not exist.

Create: It creates a new file.


CreateNew: It specifies to the operating system, that it should

create a new file.

Open: It opens an existing file.


OpenOrCreate: It specifies to the operating system that it

should open a file if it exists, otherwise it should create a new


file.

Truncate: It opens an existing file and truncates its size to zero


bytes.

FileAccess :-FileAccessenumerators have members:


Read,ReadWriteandWrite.

FileShare:-FileShareenumerators have the following members:


Inheritable: It allows a file handle to pass
inheritance
to the child processes
None: It declines sharing of the current file
Read: It allows opening the file for reading
ReadWrite: It allows opening the file for reading and
writing
Write: It allows opening the file for writing

CREATE A FILE IN VB.NET

ToCreate a File in VB.NET,theFileStream


Classis used.
The member of this class FileAccess, FileMode,
FileShare enumerations along with constructors
are used to open or create or share a file.
Then using the classesStreamReader,
StreamWriterare used to modify the file data

EXAMPLE:
Imports System.IO
Module Module1
Sub Main()
Dim fs As New System.IO.FileStream("file.txt",
FileMode.Create, FileAccess.Write)
Dim w As StreamWriter(fs)
w.BaseStream.Seek(0, SeekOrigin.End)
w.WriteLine("Here is the file's text.")
w.Write("Here is more file text." &
ControlChars.CrLf)
w.WriteLine("And that's about it.")
w.Flush()
w.Close()
End Sub
End Module

OPENING A FILE IN VB.NET

Toopen a file, the File.OpenText method of


theFileStreamclass is used along with
theStreamReaderclass.

Files are opened by default in synchronous


mode, by using contructors files can also be
opened asynchronously.

EXAMPLE:
Imports System.IO
Module Module1
Sub Main()
Dim r As StreamReader
r = File.OpenText("c:\sample1.txt")
Console.WriteLine(r.ReadToEnd)
Console.Read()
r.Close()
End Sub
End Module

WRITE A FILE IN VB.NET

Towrite a file, theStreamWriterclass


andFile.CreateTextmethod is used to
create a file first.

Then theWriteLinemethod is used to write


text.

EXAMPLE:
Imports System.IO
Module Module1
Sub Main()
Dim w As StreamWriter
Dim r As StreamReader
w = File.CreateText("C:\write.txt")
w.WriteLine("Welcome to.Net Programming")
w.WriteLine("This is an example to write to a File")
w.Flush()
w.Close()
r = File.OpenText("c:\write.txt")
Console.WriteLine(r.ReadToEnd)
Console.Read()
r.Close()
End Sub
End Module

Вам также может понравиться