Files and Streams.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Reading and Writing Text Files Svetlin Nakov Telerik Corporation
File Handling Advanced Higher Programming. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a.
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
Chapter 11 Data Files Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
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.
ASP.NET Programming with C# and SQL Server First Edition
MIS316 – BUSINESS APPLICATION DEVELOPMENT – Chapter 14 – Files and Streams 1Microsoft Visual C# 2012, Fifth Edition.
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.
Computer and Programming File I/O File Input/Output Author: Chaiporn Jaikaeo, Jittat Fakcharoenphol Edited by Supaporn Erjongmanee Lecture 13.
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.
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.
File I/O 11_file_processing.ppt
1 14/10/58Dr.Mud-Armeen Munlin 1 Files and Streams ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Chapter Ten Structures and Sequential Access Files Programming with Microsoft Visual Basic th Edition.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
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.
Object Oriented Software Development 10. Persistent Storage.
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.
BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
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.
Introduction to VB programming Dr. John P. Abraham UTPA Chapters 2 & 3.
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.
Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY.
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.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
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.
IS 350 Application Structure
Reading & writing to files
The Visual Basic .NET Coach
Files.
18 Files and Streams.
I/O Basics.
How to work with files and data streams
Files and Streams.
Files and Streams Lect3 CT1411.
File Input/Output (I/O)
Topics Introduction to File Input and Output
Chapter 7 Files and Exceptions
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Files & Streams.
Chapter 3.5 Input and Output
Input and Output.
Files and Streams Lect10 GC201 12/1/2015.
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to File Input and Output
BO65: PROGRAMMING WRITING TO TEXT FILES.
How to work with files and data streams
Introduction to Programming Lecture 6
Input and Output.
Topics Introduction to File Input and Output
Chapter 11 Saving Data and Objects In Files
Input and Output Chapter 3.5
Presentation transcript:

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 data

Introduction to Files and Streams System.IO classes are used to work with files and streams File - block of information stored on disk or another media Text file - file that contains lines of written information that can be sent directly to the screen or printer Binary file - file that contains bits that do not necessarily represent printable text Examples: Word file, machine language file You can work with two kinds of files – text files (containing only characters) and binary files

Text vs Binary Files A text file displayed in a text editor A binary file displayed a text editor

Introduction to Processing Textual Data One way to process textual files is from beginning to end using sequential access .This type of file is called a sequential file Sequential files are useful for: Storing text Easy implementation in programs Where real-time editing of file(s) is not required

Introduction to Processing Textual Data Sequential files can be categorized into roughly three types Free-form files have no particular format Fields in a delimited file are separated with a special character called a delimiter In a fixed-field file, each field occupies the same character positions in every record

Introduction to Processing Textual Data Sequential files can be read one character at a time, one line at a time, or the entire file can be read at once Sequential files are typically read into a string or an array

The Directory Class We need to add Imports System.IO Common methods of the Directory class Exists (path designation) CreateDirectory (path designation) Delete (path designation) Code that uses some of the Directory methods Dim dir As String = "C:\VB 2005\Files\" If Not Directory.Exists(dir) Then Directory.CreateDirectory(dir) End If

The Directory Class

The Directory Class The default location where the files we create are saved is the bin directory of the Windows Application with which we are working.

The File Class Common methods of the File class Exists (path) Delete (path) Copy (source, dest) Move (source, dest) Code that uses some of the File methods Dim dir As String = "C:\VB 2005\Files\" Dim path As String = dir & "Products.txt" If File.Exists(path) Then File.Delete(path) End If

The File Class

The File Class

Files are manipulated in 3 stages File Open : If the file does not exist it is created and then opened by the operating system. A portion of memory (RAM) is reserved by the Operating System. Process File : When a file is open it can be written to or read from. Close File : When a file has been opened and processed it must then be closed. The Operating system will then release the memory.

The Stream-File Connection To perform file processing in Visual Basic, namespace System.IO must be referenced. This namespace includes definitions for stream classes such as: FileStream StreamReader StreamWriter BinaryReader BinaryWriter Files are opened by creating objects of these stream classes.

Establishing Connections There are several different ways to establish file-stream connections Create a StreamReader object Create a StreamWriter object Create a FileStream object The results of using these techniques are similar – they all result in the creation of (or opening of) a stream (fs) against which all subsequent file operations are written

The StreamReder Class The StreamReader and StreamWriter classes belong to the System.IO namespace The StreamReader constructor accepts one argument – the path and filename of the sequential file to open. Dim CurrentReader As StreamReader = New _ System.IO.StreamReader("C:\Demo.txt") The Close method closes a sequential file. Always close files when processing is complete to prevent loss of data Open files also consume system resources Example: CurrentReader.Close()

The StreamReder Class The Read method reads a single character or many characters. Without arguments, the Read method returns the Integer code point of the character read The ReadLine method reads a record. The carriage return at the end of the record is discarded. The method returns a String containing the characters read. The ReadToEnd method reads from the current file position to the end of the file. The method returns a String containing the characters read

Example read()

Example :Reading Entire Content of File

Reading a Sequential File One Record at a Time

The StreamWriter Class The StreamWriter class of the System.IO namespace writes a sequential file The constructor accepts one argument – the file to write Example: Dim CurrentWriter As New _ System.IO.StreamWriter("C:\Demo.txt") ' Statements to write the file. CurrentWriter.Close()

The StreamWriter Class The NewLine property contains the character(s) that mark the end of the line The Close method closes the sequential file It's imperative to close a sequential file once writing is complete to prevent loss of data The Write method writes a character or array of characters The WriteLine method writes data terminated by the character(s) stored in the NewLine property If the data type passed to Write or WriteLine is not a string, these methods will call toString Individual variables must be concatenated and separators must be used

StreamWriter Example

StreamWriter Example Imports System.IO Module Module1 Sub Main() Dim path As String = "C:\testfolder\" Dim filename As String = path & "test.txt" Dim writer As New System.IO.StreamWriter(filename) Dim filecontent As String filecontent = Console.ReadLine() writer.WriteLine(filecontent) writer.Close() End Sub End Module

Writing a Freeform File A freeform file can be written all at once as follows: Dim StringData As String = "Freeform text" Dim CurrentWriter As New _ System.IO.StreamWriter("C:\Demo.txt") CurrentWriter.Write(StringData) CurrentWriter.Close()

Writing a Delimited File

Example

Imports System.IO Public Class Form1 Dim filecontent As String Dim fn As String = "test1.txt" Dim fs As FileStream Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click fs = New FileStream(fn, FileMode.OpenOrCreate, FileAccess.Write) Dim writer As New StreamWriter(fs) filecontent = TextBox1.Text writer.WriteLine(filecontent) writer.Close() fs.Close() MsgBox("writing complete") End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click fs = New FileStream(fn, FileMode.Open, FileAccess.Read) Dim reader As New StreamReader(fs) filecontent = reader.ReadLine() TextBox2.Text = filecontent reader.Close() End Class

if exist "$(TargetPath).locked" del "$(TargetPath).locked" if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"