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.

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.
1 Streams and Input/Output Files Part 3. 2 Handling Primitive Data Types The basic input and output streams provide read/write methods that can be used.
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
Jan Java I/O Yangjun Chen Dept. Business Computing University of Winnipeg.
Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
Exceptions. Definition Exception: something unexpected that can occur in the execution of a program e.g., divide by zero or attempt to open a file that.
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.
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
©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 
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 11: 1 CMT1000: Introduction to Programming Ed Currie Lecture 10: File Input.
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,
File I/O in Java CS 311, Winter File Basics Recall that a file is block structured. What does this mean? What happens when an application opens.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 12 File Input and Output.
CS102--Object Oriented Programming Lecture 14: – File I/O BufferedReader The File class Write to /read from Binary files Copyright © 2008 Xiaoyan Li.
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
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 ->
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
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/
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.
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.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
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.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Two Ways to Store Data in a File  Text format  Binary format.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 11 GEORGE KOUTSOGIANNAKIS Copyright: 2015 / Illinois Institute of Technology/George Koutsogiannakis 1.
Read and Write Files  By the end of this lab you will be able to:  Write a file in internal storage  Read a file from internal storage  Write a file.
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.
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.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
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.
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 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
OO Design and Programming II I/O: Reading and Writing
Introduction to programming in java
File handling and Scanning COMP T1
University of Central Florida COP 3330 Object Oriented Programming
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
I/O Streams- Basics Byte Streams and Character Streams
Streams and File I/O Chapter 14.
JAVA IO.
Reading and Writing Text Files
OBJECT ORIENTED PROGRAMMING II LECTURE 20 GEORGE KOUTSOGIANNAKIS
Web Design & Development Lecture 8
EEC 484/584 Computer Networks
David Davenport Spring 2005
Presentation transcript:

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 binary input/output of data. Byte streams are used where the program needs to work with the raw binary data.

Byte streams All byte stream classes are descended from InputStream and OutputStream.InputStreamOutputStream

The java.io package contains two classes, InputStream and OutputStream, from which most of the other classes in the package are derived.

InputStream The InputStream class defines methods for reading bytes or arrays of bytes, marking locations in the stream, skipping bytes of input, finding out the no of bytes available for reading, and resetting the current position within the stream. An input stream is automatically opened when you create it. You can explicitly close a stream with the close method, or let it be closed implicitly when the InputStream is garbage collected.

OutputStream OutputStream defines methods for writing bytes or arrays of bytes to the stream. An output stream is automatically opened when you create it. You can explicitly close an output stream with the close method, or let it be closed implicitly when the OutputStream is garbage collected

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class stream1 { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(“c:\\mytext.txt"); out = new FileOutputStream("e:\\outagain.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }

Character Streams Character streams works with the characters. In Java characters are stored as Unicode conventions. This kind of storage makes it platform independent, program independent, language independent. Characters storage using Unicode convention makes the Java ready for internationalization and for this Java provides the Character stream I/O.

import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class stream2 { public static void main(String[] args) throws IOException { FileReader iStream = null; FileWriter oStream = null; try { iStream = new FileReader("e:\\Thanks.txt"); oStream = new FileWriter("e:\\outagain.txt"); int c; while ((c = iStream.read()) != -1) { oStream.write(c); } } finally { if (iStream != null) { iStream.close(); } if (oStream != null) { oStream.close(); } } } }

Unbuffered I/O This means each read or write request is handled directly by the underlying OS. This can make a program much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive.

Buffered I/O streams Buffered input streams read data from a memory area known as a buffer; the native input API is called only when the buffer is empty. Buffered output streams write data to a buffer, and the native output API is called only when the buffer is full.

A program can convert an unbuffered stream into a buffered stream using the wrapping idiom BufferedReader inputStream; inputStream = new BufferedReader(new FileReader (“c:\\mytext.txt")); BufferedWriter outputStream; outputStream = new BufferedWriter(new FileWriter (“c:\\charoutput.txt"));

Scanning and Formatting Programming I/O often involves translating to and from the neatly formatted data. Scanner are useful for breaking down formatted input into tokens and translating individual tokens according to their data type – By default, a scanner uses white space to separate tokens. Formatting – Stream objects that implement formatting are instances of either PrintWriter, a character stream class, or PrintStream, a byte stream class.PrintWriterPrintStream – print, println format and format formats

import java.io.*; import java.util.Scanner; public class stream3 { public static void main(String[] args) throws IOException { Scanner s = null; try { s = new Scanner(new BufferedReader(new FileReader("e:\\links.txt"))); while (s.hasNext()) { System.out.println(s.next()); } } finally { if (s != null) { s.close(); }

I/O from Command Line The Java platform supports three Standard Streams: Standard Input, accessed through System.in; Standard Output, accessed through System.out; and Standard Error, accessed through System.err. These objects are defined automatically and do not need to be opened. Standard Output and Standard Error are both for output; having error output separately allows the user to divert regular output to a file and still be able to read error messages

To use Standard Input as a character stream, wrap System.in in InputStreamReader. InputStreamReader cin = new InputStreamReader (System.in);

import java.io.*; public class stream5 { public static void main(String args[]) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); System.out.print("Enter a number "); String s1 = br.readLine(); int x = Integer.parseInt(s1); int x2=x*x; System.out.print("\nThe square of number is :"+x2); } }

import java.io.*; public class stream4 { public static void main(String args[]) throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); int marks[] = new int[3]; for(int i = 0; i < marks.length; i++) { System.out.println("Enter a whole number"); String s1 = br.readLine(); int x = Integer.parseInt(s1); marks[i] = x; } int total = 0; System.out.print("The elements are: "); for(int i = 0; i < marks.length; i++) { System.out.print(marks[i]+"\t"); total += marks[i]; } System.out.print("\nThe average is : " + (total/marks.length)); } }

import java.io.*; public class stream4 { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int marks[] = new int[3]; for(int i = 0; i < marks.length; i++) { System.out.println("Enter a whole number"); String s1 = br.readLine(); int x = Integer.parseInt(s1); marks[i] = x; } int total = 0; System.out.print("The elements are: "); for(int i = 0; i < marks.length; i++) { System.out.print(marks[i]+"\t"); total += marks[i]; } System.out.print("\nThe average is : " + (total/marks.length)); } }