I/O Streams- Basics Byte Streams and Character Streams

Slides:



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

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.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
10-1 Writing to a Text File When a text file is opened in this way, a FileNotFoundException can be thrown – In this context it actually means that the.
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.
Geoff Holmes Overview IO Zoo Stream I/O File I/O Buffering Random-Access Text Streams Examples Serialization Java IO – programs that start with import.
1 Lecture 4 Exception Handling. 2 Exception-Handling Fundamentals An exception is an abnormal condition that arises in a code sequence at run time A Java.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 12  File Input and Output Stream Classes Text Input and Output.
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
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.
Chapter 8: Exceptions and I/O Streams Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002,
Reading Information From the User Making your Programs Interactive.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
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.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
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 ->
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Io package as Java’s basic I/O system continue’d.
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Input/Ouput and Exception Handling. 2 Exceptions  An exception is an object that describes an unusual or erroneous situation  Exceptions are thrown.
Two Ways to Store Data in a File Text format Binary format.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
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.
Overloading There is another aspect to polymorphism: Overloading Overloading is not overriding. In Turkish: Overridding: eskisini (geçersiz kılmak) Overloading:
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Java Programming: Advanced Topics 1 Input/Output and Serialization Chapter 3.
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.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
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.
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.
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.
 Learn about computer files  Use the Path class  Learn about  Streams  Buffers  file organization  Use Java’s IO classes to write to and read from.
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.
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.
Java Input and Output. Java Input  Input is any information needed by your program to complete its execution  So far we have been using InputBox for.
Input Characters from the Keyboard tMyn1 Input Characters from the Keyboard We have been using console output, but not console (keyboard) input. The main.
Java Input / Output l a modular approach to input/output: - different stream objects are connected/wrapped to handle I/O l a data stream object: a flow.
Java Programming: Advanced Topics 1 Input/Output and Serialization.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Lecture 8: I/O Streams types of I/O streams Chaining Streams
Streams & File Input/Output (I/O)
CMSC 202 Text File I/O.
Objectives You should be able to describe: Interactive Keyboard Input
University of Central Florida COP 3330 Object Oriented Programming
Strings and File I/O.
Interactive Standard Input/output
Objects, Classes, Program Constructs
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
File.
Programming in Java Files and I/O Streams
Exceptions Exception handling is an important aspect of object-oriented design Chapter 10 focuses on the purpose of exceptions exception messages the.
CSS 161: Fundamentals of Computing
Input and Output Stream
I/O and Applet from Chapter 12
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Exception Handling Contents
ECE 122 April 14, 2005.
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:

I/O Streams- Basics Byte Streams and Character Streams

I/O Basics Not much use has been made of I/O in the example programs. In fact, aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real applications of Java are not text-based, console programs.

I/O Basics-Cont... Text-based console I/O is just not very important to Java programming. The preceding paragraph notwithstanding, Java does provide strong, flexible support for I/O as it relates to files and networks. Java’s I/O system is cohesive and consistent. In fact, once you understand its fundamentals, the rest of the I/O system is easy to master.

I/O Basics-Cont... Rather, they are graphically oriented programs that rely upon Java’s Abstract Window Toolkit (AWT) or Swing for interaction with the user. Although text-based programs are excellent as teaching examples, they do not constitute an important use for Java in the real world. Also, Java’s support for console I/O is limited and somewhat awkward to use—even in simple example programs.

Streams Java programs perform I/O through streams. A stream is an abstraction that either produces or consumes information. A stream is linked to a physical device by the Java I/O system. All streams behave in the same manner, even if the actual physical devices to which they are linked differ.

Streams-Cont... Thus, the same I/O classes and methods can be applied to any type of device. This means that an input stream can abstract many different kinds of input: from a disk file, a keyboard, or a network socket. Likewise, an output stream may refer to the console, a disk file, or a network connection.

Streams-Cont... Streams are a clean way to deal with input/output without having every part of your code understand the difference between a keyboard and a network, for example. Java implements streams within class hierarchies defined in the java.io package.

Byte Streams and Character Streams Java defines two types of streams: byte and character Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary data. Character streams provide a convenient means for handling input and output of characters.

Byte Streams and Character Streams-Cont... They use Unicode and, therefore, can be internationalized. Also, in some cases, character streams are more efficient than byte streams. The original version of Java (Java 1.0) did not include character streams and, thus, all I/O was byte-oriented.

Byte Streams and Character Streams-Cont... Character streams were added by Java 1.1, and certain byte-oriented classes and methods were deprecated. This is why older code that doesn’t use character streams should be updated to take advantage of them, where appropriate. One other point: at the lowest level, all I/O is still byte-oriented.

Byte Streams and Character Streams-Cont... The character-based streams simply provide a convenient and efficient means for handling characters. An overview of both byte-oriented streams and character-oriented streams is presented in the following sections.

The Byte Stream Classes Byte streams are defined by using two class hierarchies. At the top are two abstract classes: InputStream and OutputStream. Each of these abstract classes has several concrete subclasses that handle the differences between various devices, such as disk files, network connections, and even memory buffers.

The Byte Stream Classes-Cont... The byte stream classes are shown in Table 13-1. Remember, to use the stream classes, you must import java.io The abstract classes InputStream and OutputStream define several key methods that the other stream classes implement.

The Byte Stream Classes-Cont... Two of the most important are read( ) and write( ), which, respectively, read and write bytes of data. Both methods are declared as abstract inside InputStream and OutputStream. They are overridden by derived stream classes.

TABLE 13-1 The Byte Stream Classes

TABLE 13-1 The Byte Stream Classes-Cont...

The Character Stream Classes Character streams are defined by using two class hierarchies. At the top are two abstract classes, Reader and Writer. These abstract classes handle Unicode character streams. Java has several concrete subclasses of each of these. The character stream classes are shown in Table 13-2.

The Character Stream Classes-Cont... The abstract classes Reader and Writer define several key methods that the other stream classes implement. Two of the most important methods are read( ) and write( ), which read and write characters of data, respectively. These methods are overridden by derived stream classes.

Table 13-2. The Character Stream I/O Classes

Table 13-2. The Character Stream I/O Classes-Cont...

The Predefined Streams As you know, all Java programs automatically import the java.lang package. This package defines a class called System, which encapsulates several aspects of the run-time environment. For example, using some of its methods, you can obtain the current time and the settings of various properties associated with the system.

The Predefined Streams-Cont... System also contains three predefined stream variables: in, out, and err. These fields are declared as public, static, and final within System. This means that they can be used by any other part of your program and without reference to a specific System object.

The Predefined Streams-Cont... System.out refers to the standard output stream. By default, this is the console. System.in refers to standard input, which is the keyboard by default. System.err refers to the standard error stream, which also is the console by default. However, these streams may be redirected to any compatible I/O device.

The Predefined Streams-Cont... System.in is an object of type InputStream; System.out and System.err are objects of type PrintStream. These are byte streams, even though they typically are used to read and write characters from and to the console. As you will see, you can wrap these within characterbased streams, if desired. End of class

Classes used for Reading Information Scanner (specific to JAVA 5 and beyond) Import java.util.*; BufferedReader (has always been there) Import java.io.*;

Scanner Class -The Methods Constructor Scanner sc = new Scanner(System.in); Reading a String String sc.nextLine( ); Reading an int int sc.nextInt( ); Reading a double double sc.nextDouble( );

java.util.Scanner -the Steps At the top must import java.util.Scanner; Declare a Scanner object within the variable declaration area: Scanner scan = new Scanner(System.in); Now you can use it to read information from the user System.in refers to The users’ keyboard input

java.util.Scanner -more Steps Declare a variable to store the input String sName; System.out.print(“What is your name? “ ); sName = scan.nextLine( ); System.out.println(“Nice to meet you “ + sName); System.in refers to The users’ keyboard input

Reading integers with the Scanner class Reading an int number int iAge = 0; Int iYear = 2006; System.out.print(“How old are you? “); iAge = scan.nextInt( ); iYear = iYear - iAge; System.out.println(“\nSo you were born around “ + iYear);

Reading doubles with the Scanner Class Reading a double number final double dGoal = 1000000; //constant double dEarnings = 0; double dMissing = 0; System.out.print(“How much do you make per hour? “); dEarnings = scan.nextDouble( ); dMissing = dGoal - dEarnings; System.out.println(“\nIn one hours you will only need “ + iMissing + “more to earn your goal”);

java.io.BufferedReader The BufferedReader class can only read Strings However we can transform those Strings into numbers of any kind. This one is always safe.

Two Objects and one Exception In the class: public static void main(String args[ ]) throws IOException{ InputStreamReader isr = new InputStreamReader (System.in); BufferedReader br = new BufferedReader(isr); The method: String readLine( )

Reading a String with BufferedReader InputStreamReader isr = new InputStreamReader (System.in); BufferedReader br = new BufferedReader(isr); String sName; System.out.print(“What is your name? “); sName = br.readLine( ); System.out.println(“So your name is “ + sName);

Reading integers with BufferedReader String sPlaceHolder; int iAge =0; int iYear = 2006; System.out.print(“How old are you? “); sPlaceHolder = br.readLine( ); iAge = Integer.parseInt(sPlaceHolder); System.out.println(“So you are “ + iAge + “ years old”); System.out.println(“you were probably born in “+ (iYear - iage));

Reading doubles with BufferedReader Reading a double number final double dGoal = 1000000; //constant double dEarnings = 0; double dMissing = 0; System.out.print(“How much do you make per hour? “); sPlaceHolder = br.readLine( ) dEarnings = Double.parseDouble(sPlaceHolder); dMissing = dGoal - dEarnings; System.out.println(“\nIn one hours you will only need “ + iMissing + “more to earn your goal”);