Introduction to Programming Lecture 6

Slides:



Advertisements
Similar presentations
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Ticket Information Application.
Advertisements

Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Driving Test 1 Marking Scheme Focus on five areas to pass driving test 1.
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.
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.
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.
Understanding Input/Output (I/O) Classes Lesson 5.
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.
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.
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 Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Creating Sequential Files, Adding Data, & Deleting Data.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
1 Visual Basic Strings: Left$, Mid, Replace Files: Reading and Writing.
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.
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.
BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.
Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Ten Structures and Sequential Access Files.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
Compunet Corporation1 Programming with Visual Basic.NET Input and Output Files Lecture # 6 Tariq Ibn Aziz.
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.
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.
VB.Net. Topics Introduction to VB.Net Creating VB.Net Project in VS 2005 Data types and Operators in VB.Net String Functions Conditions (If Else, Select.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 6: Creating Windows–based Applications 1 Microsoft® Visual C# 2008.
Introduction to Programming Lecture 2
Reading & writing to files
CSCI 3327 Visual Basic Chapter 11: Files and Streams
Files.
18 Files and Streams.
Introduction to Programming Lecture 5
How to work with files and data streams
Files and Streams.
Files and Streams Lect3 CT1411.
Sequential Input and Output using Text Files
File Input/Output (I/O)
File Handling Programming Guides.
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
Files & Streams.
The University of Texas – Pan American
Chapter 3.5 Input and Output
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.
Visual Basic 6 Programming.
Tutorial 9 Sequential Access Files and Printing
Files and Streams Lect10 GC201 12/1/2015.
Dictionary Builder Part 4 Loading Files.
CIS16 Application Development and Programming using Visual Basic.net
BO65: PROGRAMMING WRITING TO TEXT FILES.
How to work with files and data streams
Files and Streams.
Topics Introduction to File Input and Output
Presentation transcript:

Introduction to Programming Lecture 6 Msury Mahunnah, Department of Informatics, Tallinn University of Technology

“Operations With Text Files”

Text Files Text files have an extension that ends with .txt It is called a Sequential File

Working with files File handling in Visual Basic is based on System.IO namespace with a class library that supports string, character and file manipulation. These classes contain properties, methods and events for creating, copying, moving, and deleting files. The most commonly used classes are FileStream, BinaryReader, BinaryWriter, StreamReader and StreamWriter.

You can open a Text File for ... Reading Writing Appending

Opening a Text File for Reading Const fname$ = “C:\Users\Owner\Documents\test.txt“ Dim objReader As New System.IO.StreamReader(fname) After this sentence the file is not opened yet “As New” – This means “Create a New Object”. The type of the object is a StreamReader: System.IO.StreamReader

Opening a Text File for Reading One of the StreamReader methods available for reading is “ReadToEnd” (reading whole file at once) This method read the whole text, right to the end. i.e. TextBox1.Text = objReader.ReadToEnd (After this sentence the file is opened and the text is read) It is imporant to close the StreamReader after reading (otherwise you’ll get error messages) objReader.Close()

ReadToEnd example Const fname$ = “C:\Users\Owner\Documents\test.txt" If System.IO.File.Exists(fname) = True Then Dim objReader As New System.IO.StreamReader(fname) richtextbox1.AppendText(objReader.ReadToEnd) objReader.Close() Else MsgBox(“File Does Not Exist”) End If

Reading Line by Line Method ReadToEnd is used for reading the whole file at once Method ReadLine for reading one line at a time To go through the whole file, use Do ... Loop statement with Peek method Peek signifies the end of the text file. Returns the next available character but does not consume it. If Peek returns -1 the reading sequence (process) is at the end of the file.

ReadLine example Const fname$ = “C:\Users\Owner\Documents\test.txt“ If System.IO.File.Exists(fname) = True Then Dim objReader As New System.IO.StreamReader(fname) Dim TextLine As String = “” Do While objReader.Peek() <> -1 TextLine = TextLine & objReader.ReadLine() & vbNewLine Loop richtextbox1.AppendText(TextLine) objReader.Close() Else MsgBox(“File Does Not Exist”) End If

Opening a Text File for Writing Const fname$ = “C:\Users\Owner\Documents\test2.txt" Dim objWriter As New System.IO.StreamWriter(fname) After this sentence the file is not opened yet The type of the object is a StreamWriter: System.IO.StreamWriter

Writing into Text File Method Write add text into Text File. VB insists that the file must exist before it can actually do something with it! objWriter.Write(“Hello world”) or objWriter.WriteLine(“Hello world”) - This sentence writes “Hello word” into file and goes to new line

Write example Const fname$ = “C:\Users\Owner\Documents\test.txt“ If System.IO.File.Exists(fname) = True Then Dim objWriter As New System.IO.StreamWriter(fname) objWriter.Write(“Hello world!”) objWriter.Close() MsgBox(“Text written to file!”) Else MsgBox(“File Does Not Exist”) End If

Creating a file if it doesn’t exist Const fname$ = “C:\Users\Owner\Documents\test.txt“ Dim objWriter As New System.IO.StreamWriter(fname, False) objWriter.Write(“Hello world!”) objWriter.Close() MsgBox(“Text written to file!”) (Add a comma and False)

Opening a Text File for Appending Const fname$ = “C:\Users\Owner\Documents\test.txt" If System.IO.File.Exists(fname) = True Then Dim objWriter As New System.IO.StreamWriter(fname, True) objWriter.Write(“Hello world!”) objWriter.Close() MsgBox(“Text written to file!”) Else MsgBox(“File Does Not Exist”) End If (Add a comma and True)

Reading from a file and writing into an array Dim TextLine$ Dim seq As Object Dim t&=-1 Dim i&, V$() .... TextLine = objReader.ReadLine() seq = Strings.Split(TextLine, “ “) For i = Lbound(seq) To Ubound(seq) t+=1 Redim Preserve V(t) V(t) = seq(i) Next ...

All txt-files in certain folder Dim files() As String files = System.IO.Directory.GetFiles(path, pattern) files = System.IO.Directory.GetFiles(“C:\Users\John\", "*.txt") MsgBox("Found " & files.Length & " txt-files") Dim file As String For Each file In files RTB.AppendText(file & vbNewLine) Next RTB - RichTextBox

Create and delete a directory System.IO.Directory.CreateDirectory(path) System.IO.Directory.CreateDirectory(“C:\Users\John\folder1\folder2\”) Possibility to add so many subfolder as needed! System.IO.Directory.Delete(path) System.IO.Directory.Delete(“C:\Users\John\folder1\folder2\”)

Delete all txt-files in folder Dim files() As String files = System.IO.Directory.GetFiles(“C:\Users\John", "*.txt") MsgBox("Found " & files.Length & " txt-files") Dim file As String For Each file In files System.IO.File.Delete(file) Next

Retrieving the Names of All Drives in the Computer Dim drives() As String drives = System.IO.Directory.GetLogicalDrives Dim drive As String For Each drive In drives RTB.AppendText(drive & vbNewLine) Next RTB - RichTextBox