Docsity

Java Input Output-Java Programming-Lecture Slides, Slides of Computer Engineering and Programming

Partial preview of the text

Download Java Input Output-Java Programming-Lecture Slides and more Slides Computer Engineering and Programming in PDF only on Docsity!

Java Input/Output

Lecture 08

Streams

 Streams are ordered sequence of data that

have a source (input), or a destination

(output)

bytes 010011001 010011001 bytes

Basic I/O Algorithm

Reading
open a stream
while more information
read information
close the stream
Writing
open a stream
while more information
write information
close the stream

The Java™ Stream Zoo

 The java.io package
contains a collection of
stream classes that support
these algorithms for reading
and writing
 The stream classes are
divided into two class
hierarchies, based on the
data type

Character Streams (^) Byte Streams

Byte Streams

 Read and write 8-bit

bytes

 InputStream and

OutputStream are

abstract super classes

 Typically used to read

and write binary data

such as images and

sounds

Data Streams

 Grouping of classes based on their purpose is

often more convenient rather their data type, so

that streams can be cross-grouped by:

 Whether they read from and write to data ”sinks” or
 Process the information as it is being read or written

Character Streams (^) Byte Streams Data Sink Streams Processing Streams

I/O Super Classes

Writer

int write( int c); int write( char cbuf[]); int write( char cbuf[], int offset, int length)

OutputStream

int write(byte b); int write( byte buffer[]); int write( byte buffer[], int offset, int length)

 All of the streams are automatically opened when

created

 Any stream can be closed explicitly by calling its

close() method

Using File Streams import java.io.*; public class Copy { public static void main(String[] args) throws IOException { File sf = new File(“src.txt"); File of = new File("out.txt"); FileReader in = new FileReader(sf); FileWriter out = new FileWriter(of); int c; while ((c = in.read()) != -1) { out.write(c); } in.close(); out.close(); } } FileReader FileWriter in out

Using String Streams class Capitalize { public static void main(String[] args) { Reader in = new StringReader (“advance programming”); Writer out = new StringWriter(10); int nextChar = 0; while ((nextChar = in.read()) != -1) { out.write(Character.toUpperCase (( char )nextChar)); } System.out.println(out.toString()); in.close(); out.close(); } } ADVANCE PRGRAMMING

Piped Streams

 Pipes are used to channel the output from

one thread into the input of another

 With pipe streams, the output from one

method could be piped into the next

Reverse Sort Reverse Reverse Sort Reverse

Buffer Streams

 Buffer data while reading or writing, thereby reducing

the number of accesses required on the original data

source.

 More efficient than similar non-buffered streams and

are often used with other streams

 The buffer size may be specified, or default size may

be accepted

BufferedReader
BufferedWriter

Character Streams

BufferedInputStream
BufferedOutputStream

Byte Streams

Using Buffer Streams

import java.io.*; public class Copy { public static void main(String[] args) throws IOException { // opening the streams FileReader in = new FileReader (“infile.txt"); BufferedReader br = new BufferedReader(in); FileWriter out = new FileWriter ("outfile.txt"); BufferedWriter bw = new BufferedWriter(out); // processing the streams String aLine = null ; while ((aLine = br.readLine()) != null ) { bw.write(aLine, 0, aLine.length()); } // closing the streams in.close(); out.close(); } }

Using Filter StreamsDataInputStream & DataOutputStreams read and write primitive type (ints, floats, string etc.)

import java.io.*;
public class DataIODemo {
public static void main(String[] args)
throws IOException {
// write the data out
double[] prices = {19.99, 9.99, 15.99, 3.99, 4.99};
int[] units = { 12, 8, 13, 29, 50 };
String[] descs = { "Java T-shirt", "Java Mug",
"Duke Juggling Dolls",
"Java Pin", "Java Key Chain" };

Filter Streams

// write the data out DataOutputStream out = new DataOutputStream(new FileOutputStream("invoice1.txt")); for ( int i = 0; i < prices.length; i ++) { out.writeDouble(prices[i]); out.writeChar('\t'); out.writeInt(units[i]); out.writeChar ('\t'); out.writeChars (descs[i]); out.writeChar('\n'); } out.close(); double price; int unit; StringBuffer desc; double total = 0.0; // read it in again DataInputStream in = new DataInputStream( new FileInputStream("invoice.txt"));