Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3327 Visual Basic Chapter 11: Files and Streams

Similar presentations


Presentation on theme: "CSCI 3327 Visual Basic Chapter 11: Files and Streams"— Presentation transcript:

1 CSCI 3327 Visual Basic Chapter 11: Files and Streams
UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous lecture slides. – Xiang Lian

2 Objectives In this chapter, you will
Learn how to use file processing to implement a business application Get familiar with creating, writing to and reading from files Know the sequential-access file processing Use classes StreamWriter and StreamReader to write text to and read text from files

3 Data Hierarchy Bits Characters (ASCII or Unicode) Fields Records File

4 Files and Streams File as a sequential stream of bytes
Each file ends with an End of File Marker Windows keeps track of total number of bytes in a file When a file is opened an object is created and a stream is associated with the object Each program automatically gets 3 objects, console.out, console.in and console.error

5 File Processing Classes in .Net Framework
The System.IO namespace includes stream classes such as StreamReader (for text input) StreamWriter (for text output) FileStream for both input and output to file MemoryStream enables the transfer of data directly to and from memory BufferedStream uses buffering to transfer data to and from a stream

6 Classes File and Directory
Files are organized in directories Class directory provides capabilities for manipulating directories File Class methods AppendText, Copy, Create, CreateText, Delete, Exists, GetCreationTime, GetLastAccessTime, GetLastWriteTime, Move, Open, OpenRead, OpenText, OpenWrite Show Program

7 Directory Class Methods
CreateDirectory Delete Exists GetDirectories GetFiles GetCreationTime GetLastAccessTime GetLastWriteTime Move

8 Create a File to Write Imports System.IO Declare two variables
Dim fileInfo As FileStream Dim fileWriter As StreamWriter Open File fileInfo = New Filestream(filename, FileMode.Create, FileAccess.Write) fileWriter = New StreamWriter(fileInfo)

9 Write to File fileWriter.WriteLine(“text”)

10 Close File Filewriter.Close()

11 Example 8.7: CreateAccounts.vb
URL: Imports System.IO Dim fileWriter As StreamWriter Using fileChooser As New SaveFileDialog() Result = fileChooser.ShowDialog() fileName = fileChooser.FileName End Using Windows.Forms.DialogResult.Cancel fileWriter = New StreamWriter(fileName, True) fileWriter.WriteLine(“…”) fileWriter.Close() If the file does not exist, create a new file

12 Example 8.7: CreateAccounts.vb (cont'd)
Exceptions Catch ex As IOException Catch ex As FormatException

13 Struct Public Structure student Dim lastName As String
Dim firstName As String Dim tele As String Dim gpa As Decimal End Structure Public students As New List(Of student) Public oneStudent As New student

14 Opening File Dim fileResult As DialogResult
OpenFileDialog1.ShowDialog() If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then fileName = OpenFileDialog1.FileName txtFileName.Text = OpenFileDialog1.FileName End If If fileResult <> Windows.Forms.DialogResult.Cancel Then Try input = New FileStream(fileName, FileMode.Open, FileAccess.Read) filereader = New StreamReader(input) Catch ex As Exception MessageBox.Show("Error Opening File", "Error Message", _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try btnReadFile.Enabled = True

15 Reading File Dim oneline As String Dim datasplit(3) As String Try
While Not (filereader.EndOfStream) oneline = filereader.ReadLine() datasplit = oneline.Split(","c) oneStudent.firstName = datasplit(0) oneStudent.lastName = datasplit(1) oneStudent.tele = datasplit(2) oneStudent.gpa = datasplit(3) students.Add(oneStudent) End While Catch ex As Exception MessageBox.Show("File has not been open!", "File Error") End Try lblMessage.Text = "Total Number of Records Read: " & students.Count

16 Adding Records oneStudent.firstName = txtFirstName.Text
oneStudent.lastName = txtLastName.Text oneStudent.tele = txtTele.Text oneStudent.gpa=Convert.ToDecimal(txtGPA.Text) students.Add(oneStudent)

17 Example 8.14: CreditInquiry.vb
URL: Enum AccountType CREDIT DEBIT ZERO End Enum Using fileChooser As New OpenFileDialog() result = fileChooser.ShowDialog() fileName = fileChooser.FileName End Using

18 Example 8.14: CreditInquiry.vb (cont'd)
URL: fileReader = New StreamReader(fileName) Do While Not fileReader.EndOfStream Loop If fileReader IsNot Nothing Then Try fileReader.Close() Catch ex As IOException End Try End If Finally block, guaranteed to be executed

19


Download ppt "CSCI 3327 Visual Basic Chapter 11: Files and Streams"

Similar presentations


Ads by Google