CSCI 3327 Visual Basic Chapter 11: Files and Streams

Slides:



Advertisements
Similar presentations
Pemrograman VisualMinggu …12… Page 1 MINGGU Ke Duabelas Pemrograman Visual Pokok Bahasan: File and Stream Tujuan Instruksional Khusus: Mahasiswa dapat.
Advertisements

Object Oriented Programming Files and Streams Dr. Mike Spann
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
Files & Streams. Files Introduction Files are used for long-term retention of large amounts of data, even after the program that created the data terminates.
Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Creating Sequential-Access File  Serializable attribute indicates to the compiler that objects of a class can be serialized– written to or read from a.
Sequential-access file Create, read and write an object into a sequential-access file Serialize and deserialize the object to write and read from a data.
File and Streams There are many ways of organizing records in a file. There are many ways of organizing records in a file. The most common type of organization.
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
1 Binary Files ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
Visual C Sharp – File I/O - 1 Variables and arrays only temporary - lost when a program terminates Files used for long term storage (Data bases considered.
CIS 270—Application Development II Chapter 14—Files and Streams.
File I/O 11_file_processing.ppt
 2006 Pearson Education, Inc. All rights reserved Files and Streams.
1 14/10/58Dr.Mud-Armeen Munlin 1 Files and Streams ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
File I/O What We’ll Cover –Visual Basic Techniques for Text Files –.NET Techniques for Text Files What We’ll Not Cover –Binary File I/O –XML File I/O.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
CS360 Windows Programming
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams -- Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
1 Exceptions Exception handling – Exception Indication of problem during execution – E.g., divide by zero – Chained exceptions Uses of exception handling.
CSC 298 Streams and files.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
Chapter 21 – Files and Streams 1 Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
Ajay Tripathi Input Output. Ajay Tripathi Input/output (IO) refers to the operations for reading and writing data to streams and files. In the.NET Framework,
Introduction to VB programming Dr. John P. Abraham UTPA Chapters 2 & 3.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Files and Streams. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
C# Programming: From Problem Analysis to Program Design
CSCI 3327 Visual Basic Chapter 11: Files and Streams
Files.
18 Files and Streams.
Lectures 12 Files & Streams Dr. Eng. Ibrahim El-Nahry.
How to work with files and data streams
Files and Streams.
Files and Streams Lect3 CT1411.
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
Sequential Input and Output using Text Files
File Input/Output (I/O)
CSCI 3327 Visual Basic Chapter 7: Data Manipulation in Arrays
CSCI 3327 Visual Basic Chapter 7: Arrays
טיפול בקבצים ליווי מקצועי : ארז קלר
Files & Streams.
The University of Texas – Pan American
CSCI 3327 Visual Basic Review: Final Exam
CSCI 3328 Object Oriented Programming in C# Chapter 8: LINQ and Generic Collections UTPA – Fall 2012 This set of slides is revised from lecture slides.
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams -- Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides.
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Object Oriented Programming
Files and Streams Lect10 GC201 12/1/2015.
CSCI 3328 Object Oriented Programming in C# Review: Final Exam
CIS16 Application Development and Programming using Visual Basic.net
BO65: PROGRAMMING WRITING TO TEXT FILES.
How to work with files and data streams
CSCI 3328 Object Oriented Programming in C# Chapter 11: Files and Streams UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Reading from File & Picture Boxes
Files and Streams.
Chapter 11 Saving Data and Objects In Files
Presentation transcript:

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

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

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

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

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

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

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

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)

Write to File fileWriter.WriteLine(“text”)

Close File Filewriter.Close()

Example 8.7: CreateAccounts.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html 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

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

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

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

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

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

Example 8.14: CreditInquiry.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html Enum AccountType CREDIT DEBIT ZERO End Enum Using fileChooser As New OpenFileDialog() result = fileChooser.ShowDialog() fileName = fileChooser.FileName End Using

Example 8.14: CreditInquiry.vb (cont'd) URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html 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