Vb.net File Handling | Computer File

Please download to get full document.

View again

of 3
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report



Views: 11 | Pages: 3

Extension: PDF | Download: 1

Related documents
Vb.net File Handling
  http://www.tutorialspoint.com/vb.net/vb.net_file_handling.htm Copyright © tutorialspoint.com VB.NET - FILE HANDLINGVB.NET - FILE HANDLING A file  is a collection of data stored in a disk with a specific name and a directory path. When a fileis opened for reading or writing, it becomes a stream . The stream is basically the sequence of bytes passing through the communication path. There aretwo main streams: the input stream  and the output stream . The input stream  is used forreading data from file readoperation  and the output stream  is used for writing into the file writeoperation . VB.Net I/O Classes  The System.IO namespace has various classes that are used for performing various operationswith files, like creating and deleting files, reading from or writing to a file, closing a file, etc. The following table shows some commonly used non-abstract classes in the System.IOnamespace: I/O Class Description BinaryReaderReads primitive data from a binary stream.BinaryWriterWrites primitive data in binary format.BufferedStreamA temporary storage for a stream of bytes.DirectoryHelps in manipulating a directory structure.DirectoryInfoUsed for performing operations on directories.DriveInfoProvides information for the drives.FileHelps in manipulating files.FileInfoUsed for performing operations on files.FileStreamUsed to read from and write to any location in a file.MemoryStreamUsed for random access of streamed data stored in memory.PathPerforms operations on path information.StreamReaderUsed for reading characters from a byte stream.StreamWriterIs used for writing characters to a stream.StringReaderIs used for reading from a string buffer.StringWriterIs used for writing into a string buffer. The FileStream Class  The FileStream  class in the System.IO namespace helps in reading from, writing to and closingfiles. This class derives from the abstract class Stream. You need to create a FileStream  object to create a new file or open an existing file. The syntax forcreating a FileStream  object is as follows: Dim <object_name> As FileStream = New FileStream(<file_name>, <FileMode Enumerator>,  <FileAccess Enumerator>, <FileShare Enumerator>)  For example, for creating a FileStream object F  for reading a file named sample.txt : Dim f1 As FileStream = New FileStream( test.dat , FileMode.OpenOrCreate,  FileAccess.ReadWrite) Parameter Description FileMode The FileMode  enumerator defines various methods for opening files. Themembers of the FileMode enumerator are: Append : It opens an existing file and puts cursor at the end of file, orcreates 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 anew file. Open : It opens an existing file. OpenOrCreate : It specifies to the operating system that it should opena 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 FileAccess  enumerators have members: Read , ReadWrite  and Write .FileShare FileShare  enumerators have the following members: Inheritable : It allows a file handle to pass inheritance to the childprocesses 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 Example:  The following program demonstrates use of the FileStream class: Imports System.IO Module fileProg Sub Main()  Dim f1 As FileStream = New FileStream( test.dat , _  FileMode.OpenOrCreate, FileAccess.ReadWrite)  Dim i As Integer  For i = 0 To 20  f1.WriteByte(CByte(i))  Next i f1.Position = 0  For i = 0 To 20  Console.Write( {0} , f1.ReadByte())  Next i f1.Close()  Console.ReadKey()   End Sub End Module When the above code is compiled and executed, it produces the following result: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1 Advanced File Operations in VB.Net  The preceding example provides simple file operations in VB.Net. However, to utilize the immensepowers of System.IO classes, you need to know the commonly used properties and methods of these classes.We will discuss these classes and the operations they perform in the following sections. Pleaseclick the links provided to get to the individual sections: Topic and Description Reading from and Writing into Text filesIt involves reading from and writing into text files. The StreamReader  and StreamWriter classes help to accomplish it.Reading from and Writing into Binary filesIt involves reading from and writing into binary files. The BinaryReader  and BinaryWriter classes help to accomplish this.Manipulating the Windows file systemIt gives a VB.Net programmer the ability to browse and locate Windows files and directories. Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks