CSC 298 Streams and files.

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
Reading and Writing Text Files Svetlin Nakov Telerik Corporation
Lecture 15: I/O and Parsing
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
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.
1 Fall 2009ACS-1903 The break And continue Statements a break statement can be used to abnormally terminate a loop. use of the break statement in loops.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
Overview of Java I/O. Stream A Program often needs to read/write information from/to outer source/destination. Outer source/destination can be : –A File.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Java I/O Input: information brought to program from an external source
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
Lecture Set 12 Sequential Files and Structures Part B – Reading and Writing Sequential Files.
CIS 068 JAVA I/O: Streams and Files. CIS 068 I/O Usual Purpose: storing data to ‘nonvolatile‘ devices, e.g. harddisk Classes provided by package java.io.
Two Ways to Store Data in a File Text format Binary format.
Files and Streams. Java I/O File I/O I/O streams provide data input/output solutions to the programs. A stream can represent many different kinds of sources.
Introduction to Java CSIS 3701: Advanced Object Oriented Programming.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4.
1 14/10/58Dr.Mud-Armeen Munlin 1 Files and Streams ผศ. ดร. หมัดอามีน หมันหลิน Faculty of IST, MUT
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 9 l Streams and Simple File I/O l Exception Handling with File.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
CS360 Windows Programming
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Lecture 5 I/O and Parsing
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
BIM313 – Advanced Programming File Operations 1. Contents Structure of a File Reading/Writing Texts from/to Files File and Directory Operations 2.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
Text Files and String Processing
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Input and Output 23: Input and Output
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
1.2 Primitive Data Types and Variables
Chapter One Lesson Three DATA TYPES ©
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
Computer Programs CS 1400 Dennis A. Fairclough Version 1.1 CS 1400 Dennis A. Fairclough Version 1.1.
File Input & Output1 Streams & File I/O. Introduction (1) The Java platform includes a number of packages that are concerned with the movement of data.
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.
Object Oriented Programming Lecture 2: BallWorld.
Files and Streams Lec Introduction  Files are used for long-term retention of large amounts of data, even after the program that created the.
OO Design and Programming II I/O: Reading and Writing
Input and Output 23: Input and Output
Object Writing in files
Streams and File I/O.
Objects, Classes, Program Constructs
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
How to work with files and data streams
Files and Streams Lect3 CT1411.
Streams and File I/O Chapter 14.
Reading and Writing Text Files
Files & Streams.
CSC 143 Java Streams.
Module 2 Variables, Assignment, and Data Types
How to work with files and data streams
Presentation transcript:

CSC 298 Streams and files

Overview Topics Streams – communicating with the outside world Data representation – bits and bytes Basic C# streams

Streams C# model of communication: streams Sequence of data flowing from a source to a program, or from a program to a destination (sink) Huge variety of stream classes in System.IO Highly modular Some are data sources/sinks Others are wrapper classes that take data from a stream and transform it somehow to produce a stream with different characteristics

Stream Processing Basic idea the same for input & output // input open a stream while more data { read & process next data } close stream // output write data to stream

Opening & Closing Streams Before a stream can be used it must be opened Create a stream object and connect it to source or destination of the stream data Often done implicitly as part of creating stream objects When we’re done with a stream, it should be closed Cleans up any unfinished operations, then breaks the connection between the program and the data source/destination

Data Representation Underneath it’s all bits (binary digits – 0/1) Byte – group of 8 binary digits Smallest addressable unit of memory Meaning depends on interpretation 01000001 = int 65 = char ‘A’ 0011111 = int 63 = char ‘?’ 00111100 = int 54 = char ‘6’ But it’s still just bits

Some C# types (1) bool – 1 byte (0 = false; 1 = true) Integer types sbyte – 1 byte (-128 to 127) byte – 1 byte (0 to 255) short – 2 bytes (-32768 to 32767) ushort – 2 bytes (0 to 65535) int – 4 bytes (-231 to 231 - 1) uint – 4 bytes (0 to 232 - 1) long – 8 bytes (-263 to 263 - 1) ulong – 8 bytes (0 to 264 – 1) char – 2 bytes; Unicode characters w/decimal values 0 to 65535

C# types (2) Floating-point (decimal) types float – 4 bytes; approx. 6 decimal digits precision double – 8 bytes; approx. 15 decimal digits precision Other struct and class types depends on the struct or class definition (could be huge).

C# Streams 2 major sets of stream classes Byte streams – read/write individual bytes Corresponds to physical data – network and disk I/O streams Low-level Character streams – 2-byte Unicode characters Primary text input/output stream classes

Basic Abstract stream classes Byte stream: Stream Character streams: TextReader, TextWriter

Basic Reader/Writer Operations int Read();// return Unicode value of next // character string ReadLine();// return a line of text Convention: returns -1 if no data available (end of file) Writer void Write(char c); // write character c void WriteLine(string s);// write string s All can throw IOExceptions Normally throws a specific subclass of IOException depending on the actual error

File I/O Idea: Create a data object that manages the file stream Member functions support input and output operations Files normally opened when these objects are created Close files explicitly when done to be sure things terminate cleanly Basic classes FileStream derived from Stream StreamReader derived from TextReader StreamWriter derived from TextWriter

Copy File One byte at a Time class FileCopy { public static void Main() { FileStream inFile = new FileStream(@“c:\source.txt”, FileMode.Open, FileAccess.Read); FileStream outFile = new FileStream(@“c:\sink.txt”, FileMode.OpenOrCreate, FileAccess.Write); int b;//current byte (an int since it can be -1 for EOF) b = inFile.ReadByte(); while (b != -1) { // end of file? outFile.WriteByte(b); } inFile.Close(); outFile.Close();

Critique Problems with this? Very low-level; would like to work with more convenient chunks like lines of text Not very useful to hard-wire file names into the code What about possible exceptions? See full code on the class web site

Command Line Arguments Arguments can be supplied on command line when program is executed FileCopy c:\input.txt c:\output.txt These values are found in the String array parameter to main public static void Main(String[] args) { if (args.length != 2) { Console.WriteLine (“Must have 2 file names for arguments”); return;} // open files using names in args[0] and // args[1], etc...

Higher-Level Input – StreamReader Can be wrapped around a Stream, or can be created directly from a file Provides ReadLine() method to read next line and return it as a single stream Returns null if end-of-file reached Handles newline portability problems – don’t try to handle newlines yourself; take advantage of this FileStream inFile = new FileStream(fileName, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(inFile); String line = sr.readLine(); // Could do new StreamReader(fileName)

StreamWriter Same as StreamReader but for output WriteLine(s); // write a string on one line

Exception Handling Most I/O functions can throw subclasses of IOException Good practice – handle common cases and generate a user-friendly error message Input file not found Unable to open output file etc.

BinaryReader and Writer A wrapper of FileStream able to deal directly with basic types (int, double, etc…) Use the methods ReadBoolean, ReadInt, etc… for input Use the method Write(…) (with many overloads) for output Useful to read and write objects from and to files. Serialization is another approach to read and write objects from and to files.