Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.

Slides:



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

Lecture 15: I/O and Parsing
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
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.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
Chapter 10 File I/O Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
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.
Chapter 91 Streams and File I/O Chapter 9. 2 Announcements Project 5 due last night Project 6 assigned Exam 2 –Wed., March 21, 7:00 – 8:00 pm, LILY 1105.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Chapter 91 Streams and File I/O Chapter 9. 2 Reminders Project 6 released: due Nov 10:30 pm Project 4 regrades due by midnight tonight Discussion.
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.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Chapter 12 File Input and Output. Topics Stream Classes Files Text Input and Output JFileChooser for GUI programs Binary files.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Java I/O Input: information brought to program from an external source
Java File I/O (Continued). File I/O in Java Like every other programming language, Java supports the writing to and reading from different files with.
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.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Input / Output Chapter 13.  We use files all the time  Programs are files  Documents are files  We want them to be “permanent”  To last beyond execution.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Streams Reading: 2 nd Ed: , rd Ed: 11.1, 19.1, 19.4
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Chapter 9-Text File I/O. Overview n Text File I/O and Streams n Writing to a file. n Reading from a file. n Parsing and tokenizing. n Random Access n.
Two Ways to Store Data in a File  Text format  Binary format.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
Streams and File I/O Chapter 9. Outline Overview of Streams and File I/O Text-File I/O Using the File Class Basic Binary-File I/O Object I/O with Object.
Chapter 10 Text Files Section 10.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
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.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
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.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Parsing Text Files F Random.
Chapter 15: 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.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
Chapter 10 File I/O Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 9Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Project 6 due on Thursday March 31 (3 weeks)
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
CHAPTER 3 File Output.
IO in java.
Lecture 8: I/O Streams types of I/O streams Chaining Streams
IO in java.
CMSC 202 Text File I/O.
Object Writing in files
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
I/O Streams- Basics Byte Streams and Character Streams
Streams and File I/O Chapter 9 Chapter 9.
Streams and File I/O Chapter 14.
CSS 161: Fundamentals of Computing
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program, then the stream.
Presentation transcript:

Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008

Chapter 92 Announcements Review session for Exam 2 –Monday, March 17, 6:00-8:00 p.m. –LWSN B131 Project 6 posted –Final Submission due March 26, 2008 –Milestone Submission due March 20, 2008

Chapter 93 I/O Streams An I/O Stream represents an input source or an output destination A stream can represent many different kinds of sources and destinations - disk files, devices, other programs, a network socket - and memory arrays Streams support many different kinds of data - simple bytes, primitive data types, localized - characters, and objects Some streams simply pass on data; others manipulate and transform the data in useful ways.

Chapter 94 I/O No matter how they work internally, all streams present the same simple model to programs that use them –A stream is a sequence of bytes (data) –Data is transferred to devices by ‘streams‘ Program Data Source output - stream Program input - stream Data Source

Chapter 95 Streams JAVA distinguishes between 2 types of streams: Text – streams, containing ‘characters‘ I ‘M A STRING\n ProgramDevice Binary Streams, containing information in 8-bit chunks ProgramDevice

Chapter 96 Streams Streams in JAVA are Objects, of course ! Having –2 types of streams (text / binary) and –2 directions (input / output) –results in 4 base-classes dealing with I/O: 1.Reader: text-input 2.Writer: text-output 3.InputStream: byte-input 4.OutputStream: byte-output

Chapter 97 The Concept of a Stream A stream is a flow of data (characters, numbers, etc.). Data flowing into a program is called an input stream. Data flowing out of a program is called an output stream.

Chapter 98 The Concept of a Stream, cont. System.out is the only output stream we have used so far. Objects of class Scanner, used for keyboard input, are streams, too.

Chapter 99 Text Files and Binary Files All data in a file is stored as binary digits. –Files with contents that must be treated as sequences of binary digits are called binary files; binary files can be read only by machines.

Chapter 910 Text Files and Binary Files, cont. Sometimes, it is more convenient to think of a file’s contents as a sequence of characters. –Files with streams and methods to make them look like sequences of characters are called text files; text files can be read by people.

Chapter 911 Text Files vs Binary Files Example: writing of the integer ’42‘ TextFile: ‘4‘ ‘2‘ (internally translated to bit representations of the characters ‘4‘ and ‘2‘) Binary-File: , one byte (= 42 decimal)

Chapter 912 Binary vs. TextFiles procon Binary Efficient in terms of time and space Preinformation about data needed to understand content Text Human readable, contains redundant information Not efficient

Chapter 913 Streams

Chapter 914 Text-File Output with PrintWriter Class PrintWriter has a method println that behaves like System.out.println

Chapter 915 Text-File Output with PrintWriter, cont. class TextFileOutputDemo

Chapter 916 Text-File Output with PrintWriter, cont. A file is opened using something similar to outputStream = new PrintWriter( new FileOutputStream(“out.txt”)); –An empty file is connected to a stream. –If the named file ( out.txt, for example) exists already, its old contents are lost. –If the named file does not exist, a new empty file is created (and named out.txt, for example).

Chapter 917 Text-File Output with PrintWriter, cont. Class Printwriter has no constructor that takes a file name as an argument. So, we use class FileOutputStream to create a stream and can be used as an argument to a PrintWriter constructor. Syntax PrintWriter Output_Stream_Name = new PrintWriter (new FileOutputStream(File_Name));

Chapter 918 Text-File Output with PrintWriter, cont. The FileOutputStream constructor, and thus the PrintWriter constructor invocation can throw a FileNotFoundException, which means that the file could not be created. The PrintWriter object is declared outside the try block. –If it were declared inside the try block, it would be local to the try block.

Chapter 919 Some Methods in Class PrintWriter constructor PrintWriter(OutputStream streamObject) to create a new file new PrintWriter(new FileOutputStream(File_Name)) to append new text to an old file new PrintWriter(new FileOutputStream(File_Name, true ))

Chapter 920 Some Methods in Class PrintWriter, cont. to output to the file connected to the stream public final void println(Almost_Anything) public final void print(Almost_Anything) To close a stream’s connection to a file public void close() To flush the output stream public void flush()

Chapter 921 Use toString for Text-File Output Classes typically include a method toString. The methods println and print in class PrintWriter behave like System.out.println and System.out.print, respectively.

Chapter 922 Use toString for Text-File Output, cont. class Species

Chapter 923 Use toString for Text-File Output, cont. class TextFileObjectOutputDemo

Chapter 924 Text-file Input with BufferedReader Class BufferedReader is the preferred stream class for reading from a text file. Class BufferedReader has no constructor that takes a filename as its argument. –Class FileReader accepts a file name as a constructor argument and produces a stream that is a Reader object. –The constructor for class BufferedReader accepts a Reader object as an argument.

Chapter 925 Text-file Input with BufferedReader, cont. syntax BufferedReader Stream_Name = new BufferedReader(new FileReader(File_Name)); Methods readln and read are used to read from the file. The FileReader constructor, and thus the BufferedReader constructor invocation can throw a FileNotFoundException.

Chapter 926 Text-file Input with BufferedReader, cont. class TextFileInputDemo

Chapter 927 Some Methods in Class BufferedReader constructor BufferedReader(Reader, readerObject) to create a stream new BufferedReader(new FileReader(File_Name)) to read a line of input from the file public String readLine() throws IOException –If the read operation goes beyond the end of the file, null is returned.

Chapter 928 Some Methods in Class BufferedReader, cont. to read a single character from the file and return it as an int value public int read() throws IOException –If the read operation goes beyond the end of the file, -1 is returned. to read a single character from the file and to treat it as a character char next = (char)(inputStream.read());

Chapter 929 Programming Example: Reading a File Name from the Keyboard, cont. class TextFileInputDemo2

Chapter 930 Binary Files, cont. Class ObjectInputStream and class ObjectOutputStream are used to process binary files. –Data is read or written, one byte at a time. –Numbers and characters are converted automatically to bytes for storage in a binary file. –Data in files can be treated as Java primitive data types, as strings, or as other objects.

Chapter 931 Opening a Binary File syntax ObjectOutputStream Output_Stream_Name = new ObjectOutputStream (new FileOutputStream(File_Name)); example ObjectOutputStream myOutputStream = new ObjectOutputStream (new FileOutputStream (“myfile.dat”));

Chapter 932 Output to Binary Files Using ObjectOutputStream class BinaryOutputDemo

Chapter 933 Some Methods in Class ObjectOutputStream, cont. to write a primitive type, cont. public void writeLong(long n) throws IOException public void writeDouble(double x) throws IOException public void writeFloat(float x) throws IOException

Chapter 934 Some Methods in Class ObjectOutputStream, cont. public void writeChar(int n) throws IOException public void writeBoolean(boolean b) throws IOException to write a String public void writeUTF(String aString) throws IOException

Chapter 935 Some Methods in Class ObjectOutputStream, cont. To write an object public void writeObject(Object anObject) throws IOException, NotSerializableException, InvalidClassException to close public void close() throws IOException

Chapter 936 Array Objects in Binary Files An entire array can be saved to a binary file using objectWrite, and can be read later using objectRead. If the base type of the array is a class, the class should be serializable. All the data in an array can be outputted to a binary file using a single invocation of objectWrite.

Chapter 937 import java.io.*;//for keyboard input methods import java.util.*;//for StringTokenizer public class TotalNumbers { public static void main (String [] args) throws java.io.IOException { String str, s; int sum = 0, num; InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.print(“Enter four integers separated by spaces: “); //prompt str = br.readLine( ); StringTokenizer st = new StringTokenizer(str); while (st.hasMoreTokens( )) { s = st.nextToken( ); num = Integer.parseInt(s); sum += num; } }

Chapter 938 Quiz Write a program that will write your name to a text file.