Reading & writing to files

Slides:



Advertisements
Similar presentations
Introduction to File I/O How to read & write data to a disk file...
Advertisements

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Chapter 11 Data Files Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 9: Sequential Access Files and Printing
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 9 Files I/O: Files, Records and Fields. Basics of File Input and Output Have created both input and outputs from programs. Persistent data: What.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
File I/O 11_file_processing.ppt
Chapter 10: Structures and Sequential Access Files
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Chapter 10 Sequential Files and Structures. Class 10: Sequential Files Work with different types of sequential files Read sequential files based on the.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
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.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
Chapter 6 - VB 2005 by Schneider1 Chapter 6 – Repetition 6.1 Do Loops 6.2 Processing Lists of Data with Do Loops 6.3 For...Next Loops 6.4 A Case Study:
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
مقدمة في البرمجة Lecture 7. Write VB.net project using the for loop to calculate : 1- the sum of numbers from 1 to (A) numbers. 2- the sum of Odd numbers.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
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.
Chapter 6: Using VB.NET Supplied Classes Visual Basic.NET Programming: From Problem Analysis to Program Design.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
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 Programming For CCS 301 course Dr. Ahmad ABDELHAY.
1 Displaying Dialog Boxes Kashef Mughal. 2 Midterm Stats Here we go  Average was  Low was 116  High was 184  Mid Quarter Grade - check any.
INF230 Basics in C# Programming
INPUT AND OUTPUT.
Tutorial 10 – Class Average Application Introducing the Do…Loop While and Do…Loop Until Repetition Statements Outline Test-Driving the Class Average.
3.01 Apply Controls Associated With Visual Studio Form
Files.
3.01 Apply Controls Associated With Visual Studio Form
Ch 10 Sequential Access Files
Files and Streams.
Introducing Do While & Do Until Loops & Repetition Statements
Repeating Program Instructions
Exception Handling.
Files and Streams Lect3 CT1411.
Sequential Input and Output using Text Files
File Input/Output (I/O)
Variables and Arithmetic Operations
Microsoft Visual Basic 2005: Reloaded Second Edition
Topics Introduction to File Input and Output
Chapter 3.5 Input and Output
CIS16 Application Development and Programming using Visual Basic.net
CIS 16 Application Development Programming with Visual Basic
Exception Handling.
Tutorial 9 Sequential Access Files and Printing
Files and Streams Lect10 GC201 12/1/2015.
Chapter 6 - VB.Net by Schneider
CIS16 Application Development and Programming using Visual Basic.net
To understand what arrays are and how to use them
Introduction to Programming Lecture 6
Files and Streams.
Topics Introduction to File Input and Output
Chapter 11 Saving Data and Objects In Files
Presentation transcript:

Reading & writing to files In Visual Basic .NET

Lesson Objectives Understand how to read external files using System.IO.StreamReader Understand how to write to external files using System.IO.StreamWriter

External files Often need to access other files Data is often stored in other files – produced from other systems Need to write to files to save data Often used in controlled assessment tasks!

To read files in one go Dim objReader As New System.IO.StreamReader(filename) TextBox1.Text = objReader.ReadToEnd objReader.Close() .close() Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.

To read files line by line Uses the ReadLine()function Dim sr As New System.IO.StreamReader(filename) Do input =sr.ReadLine() Msgbox(input) Loop Until input Is Nothing Ideal for filling arrays or ListBoxes! The Peek method returns an integer value in order to determine whether the end of the file, or another error has occurred

Task 1: Reading a text file Create a simple text file using Notepad storing a list of shopping items. Note the file name and where it is saved Create new VB project with form with a multiline text box that reads the text file and displays it Use either ReadtoEnd() or ReadLine() in a loop Challenge – use System.IO.File.Exists(filename) to check the file exists before reading it

To write to a file To overwrite text: To append text: Add some text: Dim objWriter As New System.IO.StreamWriter(filename) To append text: Dim objWriter As New System.IO.StreamWriter(filename,True) Add some text: objWriter.Write(“some text”) Still need to close the file at the end objWriter.Close() .close() Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.

Imports statement If you type Imports System.IO Above the class declaration It enables you to reference the StreamReader or StreamWriter type directly Could save time? The Imports statement enables types that are contained in a given namespace to be referenced directly.

Task 2: Write to a text file Add a text box and button to your form On button click add code that writes the new value to your text file Make the filename global so it can be accessed on form load and button click Use vbCrLf or vbNewLine to make it appear on new line How would your reload text box to show the new addition? Challenge – before adding the item use .Contains() to test if the entry exists in the list already.

Learning Review Name 1 thing you have learned this lesson Name 1 thing you want to learn next lesson