INPUT AND OUTPUT STREAMS



Similar documents
The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

What is an I/O Stream?

JAVA - FILES AND I/O

Readings and References. Topic #10: Java Input / Output. "Streams" are the basic I/O objects. Input & Output. Streams. The stream model A-1.

CS 1302 Ch 19, Binary I/O

Programming in Java

AVRO - SERIALIZATION

Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich

D06 PROGRAMMING with JAVA

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

Chapter 10. A stream is an object that enables the flow of data between a program and some I/O device or file. File I/O

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Files and input/output streams

WRITING DATA TO A BINARY FILE

Stream Classes and File I/O

Today s Outline. Computer Communications. Java Communications uses Streams. Wrapping Streams. Stream Conventions 2/13/2016 CSE 132

Input / Output Framework java.io Framework

public static void main(string[] args) { System.out.println("hello, world"); } }

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Creating a Simple, Multithreaded Chat System with Java

Event-Driven Programming

1 of 1 24/05/ :23 AM

Decorator Pattern [GoF]

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. AVRO Tutorial

Java from a C perspective. Plan

Chapter 2: Elements of Java

Building a Multi-Threaded Web Server

Lesson: All About Sockets

CS506 Web Design and Development Solved Online Quiz No. 01

Object-Oriented Programming in Java

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Java Fundamental Classes Reference

Java Interview Questions and Answers

Using Files as Input/Output in Java 5.0 Applications

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

Files and Streams. Writeappropriatecodetoread,writeandupdatefilesusingFileInputStream, FileOutputStream and RandomAccessFile objects.

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Mail User Agent Project

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

AP Computer Science Java Subset

Division of Informatics, University of Edinburgh

13 File Output and Input

java Features Version April 19, 2013 by Thorsten Kracht

Chapter 3. Input and output. 3.1 The System class

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Java Network. Slides prepared by : Farzana Rahman

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Lecture 5: Java Fundamentals III

Network/Socket Programming in Java. Rajkumar Buyya

Advanced Network Programming Lab using Java. Angelos Stavrou

Quick Introduction to Java

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Introduction to Programming

An Overview of Java. overview-1

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Install Java Development Kit (JDK) 1.8

Serialization in Java (Binary and XML)

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

CS 106 Introduction to Computer Science I

Socket Programming in Java

Java CPD (I) Frans Coenen Department of Computer Science

Introduction to Java

Lecture J - Exceptions

CSE 8B Midterm Fall 2015

! "# $%&'( ) * ).) "%&' 1* ( %&' ! "%&'2 (! ""$ 1! ""3($

Chapter 1 Java Program Design and Development

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

The Interface Concept

Java Programming Fundamentals

Chapter 2 Introduction to Java programming

Application Development with TCP/IP. Brian S. Mitchell Drexel University

Objectives. Streams and File I/O. Objectives, cont. Outline. I/O Overview. Streams

Assignment 4 Solutions

Programming Languages CIS 443

J a v a Quiz (Unit 3, Test 0 Practice)

CS 111 Classes I 1. Software Organization View to this point:

AP Computer Science Java Mr. Clausen Program 9A, 9B

Part I. Multiple Choice Questions (2 points each):

Smallest Java Package? Java.applet.* having 1 class and 3 interfaces. Applet Class and AppletContext, AppletStub, Audioclip interfaces.

Crash Course in Java

Introduction to Java. CS 3: Computer Programming in Java

The Java Virtual Machine (JVM) Pat Morin COMP 3002

CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park

Week 1: Review of Java Programming Basics

This example illustrates how to copy contents from one file to another file. This topic is related to the I/O (input/output) of

Transcription:

INPUT AND OUTPUT The Java Platform supports different kinds of information sources and information sinks. A program may get data from an information source which may be a file on disk, a network connection, another program, standard input (e.g. keyboard), etc. Similarly a program may send data to an information sink which may be a file on disk, a network connection, another program, standard output (e.g. monitor), etc. In the Java I/O system, streams can be used to get and put information from an information source or to an information sink. Streams support either sequential read from an information source or sequential write to an information source. Regardless of the type of information source or sink, read and write operations are almost same. In order to read data from an information source you first create a stream by specifying the information source. Then, you read data as long as there is more data. Finally, you close the stream. Similarly, in order to write data to an information sink you first create a stream by specifying the information sink. Then, you write data as long as there is more data. Finally, you close the stream. There are actually two kinds of streams: byte oriented and character oriented. Byte oriented streams write bytes to a stream and read bytes from a stream. According to the direction of information flow, input streams or output streams are used for byte oriented streams. Input and Output streams are used when binary data is to be read and written. On the other hand, character oriented streams write characters to a stream and read characters from a stream. Readers and writers are used when using text based input and output (with proper encoding). According to the direction of information flow, readers or writers are used for character oriented streams. Stream classes are implemented in java.io package. So, in order to use streams classes without package names, your program must have the following imports statement at the top: import java.io.*; STREAMS InputStream and OutputStream are the superclasses for input and output streams, respectively. The specialized subclasses of these classes provide necessary implementation for different kinds of streams. Some of those specialized subclasses are as follows:

InputStream OutputStream Source/Sink Type FileInputStream ByteArrayInputStream FileOutputStream ByteArrayOutputStream A File on disk. File name can be passed to the constructor. A byte array in memory. A buffer of type byte array can be passed to the constructor. PipedInputStream PipedOutputStream Another thread (connect PipedInputStream to PipedOutputStream before reading/writing bytes ). Anything written from PipedOutputStream can be read from PipedInputStream. InputStream defines three methods to read data bytes as: int read() Method int read(byte[] b) int read(byte[] b, int offset, int length) reads next byte from the stream. reads k<=b.length bytes from the stream and returns the total number of bytes read. If there is enough data, stream fills the buffer b, otherwise stream puts bytes read starting from b[0]. reads k<=length bytes from the stream, puts those bytes starting from b[offset], and returns the total number of bytes read. If the end of stream is reached, the read method returns -1. It is also possible to learn how many bytes can be read from the stream using available() method. Similarly OutputStream defines three methods to write data bytes as: Method void write(int b) void write(byte[] b) void write(byte[] b, int offset, int length) writes (byte)b to the stream. writes b.length bytes in b to the stream. writes length bytes in b starting from b[offset] to the stream. In the following, a simple example program to copy a file on disk to a new file using streams is shown: import java.io.*; public class CopyFile { public static void main(string[] args) throws IOException{ FileInputStream in = new FileInputStream("input.txt");

FileOutputStream out = new FileOutputStream("output.txt"); byte b[] = new byte[8192]; int length; while((length = in.read(b))>0) out.write(b, 0, length); in.close(); out.close(); Note that, stream related operations may raise an IOException. Therefore, you should either handle IOExceptions or specify IOException in method declaration. FİLTERED INPUT AND FİLTERED OUTPUT The InputStream and OutputStream only provide necessary functionality for reading from and writing to streams. Filter streams, which accept an InputStream or an OutputStream as an argument to their constructors, enhance the functionality provided by these streams. Some of the filter streams that can be used are as follows: Filter Stream Constructor Arguments Behavior DataInputStream BufferedInputStream PushbackInputString DataOutputStream BufferedInputStream PrintOutputString InputStream InputStream and (optional) buffer size InputStream and (optional) pushback buffer size OutputStream OutputStream and (optional) buffer size OutputStream, (optional) auto flashing enabled, (optional) encoding Allows reading primitive data types and Strings from input stream. Does buffered reading for improved reading performance. Enables unreading of read bytes Allows writing primitive data types and Strings to input stream. Does buffered writing for improved reading performance. Enables writing primitive data types and strings to output stream in text format. An InputStream or OutputStream is usually wrapped by a filter stream to have a more useful interface. For example, the following code illustrates wrapping a FileOutputStream with a DataOutputStream to write primitive data types to a file: DataOutputStream s = new DataOutputStream(new FileOutputStream("test.dat"));

Then, it is possible to write primitive data types to the stream using writexxx(value), where XXX is the primitive data type name (like Integer, Char, Float, Double, etc). READERS AND WRİTERS Readers and Writers implement the similar functionality with InputStream and OutputStream with one exception: they work with characters instead of bytes. Reader and Writer are the superclasses for character oriented (text based) input and output streams, respectively. The specialized subclasses of these classes provide necessary implementation for different kinds of streams. Some of those specialized subclasses are as follows: FileReader Reader Writer Source/Sink Type CharArrayReader StringReader PipedReader FileWriter CharArrayWriter StringWriter PipedWriter A File on disk. File name can be passed to the constructor. A char array in memory. A buffer of type char array can be passed to the constructor. String in memory. A string can be passed to the constructor of StringReader and an initial size can be passed to the constructor of StringWriter. Another thread (connect PipedReader to PipedWriter before reading/writing characters). Anything written from PipedWriter can be read from PipedReader. Like InputStream, Reader defines three methods to read characters as: int read() Method int read(char[] b) int read(char[] b, int offset, int length) reads next byte from the stream. reads k<=b.length characters from the stream and returns the total number of characters read. If there is enough data, stream fills the buffer b, otherwise stream puts characters read starting from b[0]. reads k<=length characters from the stream, puts those characters starting from b[offset], and returns the total number of characters read. If the end of stream is reached, the read method returns -1. It is also possible to learn how many characters can be read from the stream using available() method. Writer defines five methods to write characters and strings as:

Method Void write(int b) void write(char[] b) writes (char)b to the stream. writes b.length characters in b to the stream. void write(char[] b, int offset, int length) writes length characters in b starting from b[offset] to the stream. void write(string s) writes string s to the stream. void write(string s, int offset, int length) writes length characters in string s starting from character at position offset to the stream. It is also possible to have a more useful interface by wrapping readers and writers by other Reader and Writer classes. For example, by wrapping a reader with BufferedReader you can read strings in addition to characters and character arrays. For example, the following code wraps a FileReader in BufferedReader to read strings line by line and prints lines to the screen: BufferedReader r = new BufferedReader(new FileReader("textfile.txt")); String s; while((s=r.readline())!=null) System.out.println(s); InputStreamReader and OutputStreamWriter classes provide a mechanism to wrap InputStreams and OutputStreams in Readers and Writers for text based I/O. STANDARD I/O Java provides three streams for standard I/O: System.in, System.out, and System.err. Since System.out and System.in are prewrapped by PrintStreams, any primitive type and strings can be directly written to the console using print(...) and println(...) methods. However, System.in is in InputStream form, and in order to read strings from the standard input (e.g. keyboard) System.in is usually wrapped by BufferedReader and InputStreamReader (to convert a stream interface to reader interface) before reading. The following code illustrates the reading from standard input and writing to standard output using System.in and System.out: import java.io.*; public class Echo { public static void main(string[] args) throws Exception{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s; while(((s=in.readline())!=null) && (s.length()>0)) System.out.println(s); The above program echoes the lines entered from standard input

to standard output until an empty line is entered. OBJECT SERİALİZATİON Object serialization enables you to convert an object into series of bytes, and reconstruct the object from series of bytes. The instances of classes implementing Serializable interface can automatically be converted to bytes and can automatically reconstructed from the bytes. To make the a class serializable, you could just add implements Serializable to the class declaration as: class MySerializableClass implements Serializable {... Since there is no method in Serializable interface, you do not need to implement anything. However, care should be taken when declaring member variables of the class. It may not be possible to serialize some sensitive data (such as a file handle). For such variables you can simply add transient keyword to avoid serialization of that variable as: transient DataType VariableName; Objects implementing the Serializable interface can be written to and read from streams using ObjectOutputStream and ObjectInputStream wrappers as: ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("output.dat")); ObjectInputStream in = new ObjectInputStream("input.dat")); Object obj; while((obj=in.readobject())!=null) out.writeobject(obj); in.close(); out.close();