Documente Academic
Documente Profesional
Documente Cultură
Basic Java
Unit 8 I/O Operations
Basic Java
Unit 8 Streams
F
F
F
F
F
F
F
F
F
F
F
Topics
Basic Java
Unit 8 Streams
Input / Output
Often programs need to bring in information from an external source or
send out information to an external destination.
Input and output, I/O for short, are fundamental to any computer
operating system or programming language.
The information can be anywhere: in a file, on disk, somewhere on the
network, in memory, or in another program.
Java support for I/O is in the form of streams.
Basic Java
Unit 8 Streams
A stream is an ordered
indeterminate length.
Input Streams
sequence
of
bytes
of
Input streams move bytes of data into a Java program from some
external source.
Output Streams
What is a Stream?
Basic Java
Unit 8 Streams
Input Stream
Data Source
(file, memory, socket)
Basic Java
Program reads
from the
stream
Program
Unit 8 Streams
Output Stream
Program writes
to the stream
Data Source
(file,memory, socket)
Program
Basic Java
Unit 8 Streams
Java.io Package
Basic Java
Unit 8 Streams
How to do I/O
import java.io.*;
1.
2.
3.
Basic Java
Unit 8 Streams
Basic Java
Unit 8 Streams
Byte
Streams
Character
Streams
Character stream
automatically
translates
Unicode to and
from the local
character set.
Programs use
byte streams to
perform input
and output of 8bit bytes.
Basic Java
Unit 8 Streams
Data Sink
Streams
Processing
Streams
Streams which
process the
information as its
being read or
written are called
processing
streams
Basic Java
Unit 8 Streams
Character Streams
Byte Streams
Data Sink
Streams
Processing
Streams
Basic Java
Unit 8 Streams
Classifications Interlinked
Class
Streams
Concept
Byte
Stream
Input
Stream
Data Sink
Stream
Processing
Stream
Character
Stream
Output
Stream
Data Sink
Stream
Character Streams
Input
Reader
Stream
Processing
Stream
Data Sink
Stream
Output
Writer
Stream
Processing
Stream
Data Sink
Stream
Byte Streams
Data Sink
Streams
Processing
Streams
Basic Java
Processing
Stream
Unit 8 Streams
Byte
Stream
Input
Stream
Character
Stream
Output
Stream
Input
Reader
Stream
Output
Writer
Stream
Data Sink Processing Data Sink Processing Data Sink Processing Data Sink Processing
Stream
Stream
Stream
Stream
Stream
Stream
Stream
Stream
Class
Concept
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 8 Streams
Character
Stream
Byte
Stream
Input
Stream
Data Sink
Stream
Processing
Stream
Output
Stream
Data Sink
Stream
Reader
Processing
Stream
Data Sink
Stream
Class
Concept
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Processing
Stream
Writer
Data Sink
Stream
Processing
Stream
Unit 8 Streams
Input
Stream
Processing Stream
Piped Input
Stream
Class
Filter Input
Stream
File Input
Stream
Buffered Input
Stream
Concept
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Data Input
Stream
Object Input
Stream
Push Back
Input
Stream
Unit 8 Streams
It declares three basic methods needed to read bytes of data from a stream.
Other methods
int available()
Returns the number of bytes that can be read (or skipped over) from this input stream
without blocking by the next caller of a method for this input stream.
void close()
Closes this input stream and releases any system resources associated with the stream.
long skip(long n)
Skips over and discards n bytes of data from this input stream.
void reset()
Repositions this stream to the position at the time the mark method was
last
called on this input stream.
Basic Java
Unit 8 Streams
Character
Stream
Byte
Stream
Input
Stream
Data Sink
Stream
Processing
Stream
Output
Stream
Data Sink
Stream
Processing
Stream
Reader
Data Sink
Stream
Processing
Stream
Class
Concept
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Writer
Data Sink
Stream
Processing
Stream
Unit 8 Streams
Output
Stream
Processing Stream
Piped
Output
Stream
Class
Concept
Copyright 2008 Pratian Technologies
www.pratian.com
Filter Output
Stream
File Output
Stream
Buffered Output
Stream
Basic Java
Data Output
Stream
Object
Output
Stream
Print
Stream
Unit 8 Streams
Writes b.length bytes from the specified byte array to this output stream.
OutputStream Class
Writes len bytes from the specified byte array starting at offset off to this output
stream.
Other methods
void flush()
Flushes this output stream and forces any buffered output bytes to be written
out.
long close()
Basic Java
Unit 8 Streams
IOException
IOException
IOException
IOException
IOException
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 8 Streams
System.out
Basic Java
Unit 8 Streams
IOExceptions!
Basic Java
Unit 8 Streams
catch(Exception e)
{
System.out.println("Could not read from System.in!...");
}
}
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 8 Streams
Byte
Stream
Input
Stream
Character
Stream
Output
Stream
Reader
Class
Concept
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Writer
Unit 8 Streams
Reader Classes
Object
Class
Concept
Reader
Processing Stream
Char Array
Reader
Input
Stream
Reader
Piped
Reader
File
Reader
Basic Java
Buffered
Reader
LineNumber
Reader
Filter
Reader
Push Back
Reader
Unit 8 Streams
Reader Class
int read()
int read(char[ ] c)
abstract int read(char[ ] c , int off , int len)
void close()
long skip(long n)
boolean ready()
void reset()
Basic Java
Unit 8 Streams
Byte
Stream
Input
Stream
Character
Stream
Output
Stream
Reader
Class
Concept
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Writer
Unit 8 Streams
Writer Classes
Processing Stream
Char Array
Writer
Piped
Writer
File
Writer
Basic Java
Output
Stream
Writer
Buffered
Writer
Filter
Writer
Printer
Writer
Unit 8 Streams
Writer Class
void flush()
long close()
Basic Java
Unit 8 Streams
Input
Stream
Concept
Processing Stream
Piped Input
Stream
Memory
Pipe
File
Filter Input
Stream
File Input
Stream
Buffered Input
Stream
Basic Java
Data Input
Stream
Object Input
Stream
Push Back
Input
Stream
Unit 8 Streams
Output
Stream
Concept
Processing Stream
Piped
Output
Stream
Memory
Pipe
File
Filter Output
Stream
File Output
Stream
Buffered Output
Stream
Basic Java
Data Output
Stream
Object
Output
Stream
Print
Stream
Unit 8 Streams
Reader
Class
Concept
Processing Stream
Char Array
Reader
Input
Stream
Reader
Piped
Reader
File
Reader
Memory
Pipe
File
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Buffered
Reader
LineNumber
Reader
Filter
Reader
Push Back
Reader
Unit 8 Streams
Class
Writer
Concept
Processing Stream
Char Array
Writer
Output
Stream
Writer
Piped
Writer
File
Writer
Memory
Pipe
File
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Buffered
Writer
Filter
Writer
Printer
Writer
Unit 8 Streams
Character Streams
Byte Streams
Memory
CharArrayReader
ByteArrayInputStream,
CharArrayWriter
ByteArrayOutputStream
StringReader
StringBufferInputStream
StringWriter
Pipe
File
PipedReader
PipedInputStream
PipedWriter
PipedOutputStream
FileReader
FileInputStream
FileWriter
FileOutputStream
Basic Java
Unit 8 Streams
File Class
Object
File
Basic Java
Unit 8 Streams
File Class
If a file does not exist, it can be created.
If the file does exist, a program can
renaming it,
deleting it,
Basic Java
Unit 8 Streams
File Class
Constructors
File(String pathname)
Creates a new File instance by converting the given
pathname string into an abstract pathname.
Methods
boolean exists()
Checks if the file or directory exists or not.
boolean canRead()
Checks whether the application can read the file.
boolean canWrite()
Checks whether the application can modify the file.
boolean createNewFile()
Creates a new, empty file if a file with this name does not
exist.
Basic Java
Unit 8 Streams
File Class
Methods
boolean delete()
Deletes the file or directory.
boolean isDirectory()
Checks whether the file denoted is a directory.
boolean isFile()
Checks whether the file denoted is a normal file.
String[ ] list()
Returns an array of strings naming the files and directories
in the directory.
Basic Java
Unit 8 Streams
File Example
import java.io.*;
class FileDemo
{
public static void main(String[] args) throws Exception
{
File file = new File("FileDemo.java");
if (file.exists())
System.out.println("The file exists...");
else
{
System.out.println("The file does not exist, creating a new file...");
file.createNewFile();
}
System.out.println("Absolute path of the file +
file.getAbsolutePath());
}
}
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 8 Streams
File Class
boolean mkdir()
boolean mkdirs()
String[ ] list()
Returns an array of strings naming the files and directories in the directory
denoted by this abstract pathname.
File[ ] listFiles()
Basic Java
Unit 8 Streams
FileInputStream
Input
Stream
Processing Stream
Piped Input
Stream
Filter Input
Stream
File Input
Stream
Buffered Input
Stream
File
Class
Concept
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Data Input
Stream
Object Input
Stream
Push Back
Input
Stream
Unit 8 Streams
FileInputStream
A FileInputStream class is used by a program to read information
from a file in bytes.
FileInputStream is meant for reading streams of raw bytes such as
image data.
Constructors
FileInputStream(File file)
FileInputStream(String name)
Basic Java
Unit 8 Streams
FileInputStream
Methods
int read()
int read(byte[ ] b)
void close()
Basic Java
Unit 8 Streams
FileOutputStream
Output
Stream
Class
Concept
Processing Stream
Piped
Output
Stream
Filter Output
Stream
File Output
Stream
Buffered Output
Stream
File
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Data Output
Stream
Object
Output
Stream
Print
Stream
Unit 8 Streams
FileOutputStream
Constructors
FileOutputStream(File file)
FileOutputStream(String name)
Basic Java
Unit 8 Streams
FileOutputStream
Methods
void write(int i)
void write(byte[ ] b)
Writes len bytes from the specified byte array starting at offset off
to this FileOutputStream.
void close()
Basic Java
Unit 8 Streams
Example
Basic Java
Unit 8 Streams
FileReader
Class
Reader
Concept
Processing Stream
Char Array
Reader
Input
Stream
Reader
Piped
Reader
File
Reader
File
Basic Java
Buffered
Reader
LineNumber
Reader
Filter
Reader
Push Back
Reader
Unit 8 Streams
FileReader
Constructors
FileReader(File file)
Creates a new FileReader for a given File to read from.
FileReader(String fileName)
Creates a new FileReader for a given file name to read from.
Methods
FileReader does not define any methods of its own, but inherits
methods from InputStreamReader and Reader classes.
Basic Java
Unit 8 Streams
FileWriter
Class
Writer
Concept
Processing Stream
Char Array
Writer
Output
Stream
Writer
Piped
Writer
File
Writer
File
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Buffered
Writer
Filter
Writer
Printer
Writer
Unit 8 Streams
FileWriter
Constructors
FileWriter(File file)
Creates a new FileWriter for a given File to read from.
FileWriter(String fileName)
Creates a new FileReader for a given file name to read from.
Methods
FileWriter does not define any methods of its own, but inherits methods from
OutputStreamReader and Writer classes.
Basic Java
Unit 8 Streams
Example
import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException
{
File inputFile = new File("farrago.txt");
//existing file
File outputFile = new File("outagain.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}
See Listing :FileReaderWriterDemo1.java
Basic Java
Unit 8 Streams
Stream Chaining
Basic Java
Unit 8 Streams
Stream Chaining
Processing Stream
Destinatio
n
DataSink
Stream
Data
Source
File,
Socket,
Console,
etc
Processing Stream 2
Processing Stream 1
Destinatio
n
DataSink
Stream
Basic Java
Data
Source
File,
Socket,
Console,
etc
Unit 8 Streams
Input
Stream
Class
Concept
Processing Stream
Piped Input
Stream
Filter Input
Stream
File Input
Stream
Filtering
Buffered Input
Stream
Buffering
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Peeking
Ahead
Data Input
Stream
Object Input
Stream
Push Back
Input
Stream
Unit 8 Streams
Output
Stream
Class
Concept
Processing Stream
Piped
Output
Stream
Filter Output
Stream
File Output
Stream
Filtering
Buffered Output
Stream
Printing
Buffering
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Data Output
Stream
Object
Output
Stream
Print
Stream
Unit 8 Streams
Class
Reader
Concept
Processing Stream
Char Array
Reader
Input
Stream
Reader
Piped
Reader
File
Reader
LineNumber
Reader
Filtering
Buffering
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Buffered
Reader
Filter
Reader
Push Back
Reader
Unit 8 Streams
Class
Writer
Concept
Processing Stream
Char Array
Writer
Output
Stream
Writer
Piped
Writer
Buffered
Writer
Filter
Writer
Printer
Writer
File
Writer
Buffering
Copyright 2008 Pratian Technologies
www.pratian.com
Filtering
Basic Java
Printing
Unit 8 Streams
Process
CharacterStreams
Byte Streams
Buffering
BufferedReader
BufferedInputStream
BufferedWriter
BufferedOutputStream
FilterReader
FilterInputStream
FilterWriter
FilterOutputStream
Peeking Ahead
PushbackReader
PushbackInputStream
Printing
PrintWriter
PrintStream
Filtering
Basic Java
Unit 8 Streams
FilterInputStream
Class
Input
Stream
Concept
Processing Stream
Piped Input
Stream
Filter Input
Stream
File Input
Stream
Buffered Input
Stream
Basic Java
Data Input
Stream
Object Input
Stream
Push Back
Input
Stream
Unit 8 Streams
FilterInputStream
BufferedInputStream
DataInputStream
PushbackInputStream
Basic Java
Unit 8 Streams
FilterOutputStream
Class
Output
Stream
Concept
Processing Stream
Piped
Output
Stream
Filter Output
Stream
File Output
Stream
Buffered Output
Stream
Basic Java
Data Output
Stream
Object
Output
Stream
Print
Stream
Unit 8 Streams
FilterOutputStream
BufferedOutputStream
DataOutputStream
PushbackOutputStream
Basic Java
Unit 8 Streams
Basic Java
Unit 8 Streams
BufferedInputStream
Class
Input
Stream
Concept
Processing Stream
Piped Input
Stream
Filter Input
Stream
File Input
Stream
Buffered Input
Stream
Basic Java
Data Input
Stream
Object Input
Stream
Push Back
Input
Stream
Unit 8 Streams
BufferedInputStream
Basic Java
Unit 8 Streams
BufferedInputStream
Constructors
BufferedInputStream(InputStream in)
BufferedInputStream(InputStream in , int size)
int read()
void reset()
void close()
Closes this input stream and releases any system resources associated
with the stream.
Basic Java
Unit 8 Streams
BufferedOutputStream
Class
Output
Stream
Concept
Processing Stream
Piped
Output
Stream
Filter Output
Stream
File Output
Stream
Buffered Output
Stream
Basic Java
Data Output
Stream
Object
Output
Stream
Print
Stream
Unit 8 Streams
BufferedOutputStream
Constructors
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out , int size)
Basic Java
Unit 8 Streams
BufferedOutputStream
void write(int b)
void flush()
Basic Java
Unit 8 Streams
Example
import java.io.*;
class BufferedStreamDemo
{
public static void main(String[] args) throws Exception
{
int c = 0;
File file1 = new File(Dream.jpg);
File file2 = new File(MyDream.jpg);
BufferedInputStream in =
new BufferedInputStream( new FileInputStream(file1));
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(file2));
while(c != -1)
{
c = in.read();
out.write();
}
in.close();
out.close();
}
See Listing :BufferedStreamDemo.java
}
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 8 Streams
Converting Streams
Processing Stream 2
Processing Stream 1
Destinatio
n
BufferedReader
DataSink
Stream
Input Stream
Reader
Data
Source
File,
Socket,
Console,
InputStream
etc
(System.in)
Basic Java
Unit 8 Streams
Converting Streams
Class
Reader
Concept
Processing Stream
Char Array
Reader
Input
Stream
Reader
Piped
Reader
File
Reader
Basic Java
Buffered
Reader
LineNumber
Reader
Filter
Reader
Push Back
Reader
Unit 8 Streams
Converting Streams
Class
Writer
Concept
Processing Stream
Char Array
Writer
Output
Stream
Writer
Piped
Writer
File
Writer
Basic Java
Buffered
Writer
Filter
Writer
Printer
Writer
Unit 8 Streams
Example
import java.io.*;
class InputStreamReaderDemo
{
public static void main(String[ ] args) throws Exception
{
String str = null;
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
str = reader.readLine();
System.out.println(str);
}
}
See
Listing
Copyright 2008 Pratian Technologies
www.pratian.com
:InputStreamReaderDemo.java
Basic Java
Unit 8 Streams
DataInputStream
Class
Input
Stream
Concept
Processing Stream
Piped Input
Stream
Filter Input
Stream
File Input
Stream
Buffered Input
Stream
Basic Java
Data Input
Stream
Object Input
Stream
Push Back
Input
Stream
Unit 8 Streams
DataInputStream
The real purpose of DataInputStream is not the read raw bytes using the
standard input stream methods, but to read and interpret multibyte data
like ints, floats, doubles, and chars written using DataOutputStream.
Constructors
DataInputStream(InputStream in)
Basic Java
Unit 8 Streams
DataInputStream
int read(byte[ ] b)
boolean readBoolean()
byte readByte()
char readChar()
double readDouble()
int readInt()
String readLine()
Basic Java
Unit 8 Streams
DataOutputStream
Class
Output
Stream
Concept
Processing Stream
Piped
Output
Stream
Filter Output
Stream
File Output
Stream
Buffered Output
Stream
Basic Java
Data Output
Stream
Object
Output
Stream
Print
Stream
Unit 8 Streams
DataOutputStream
The real purpose of DataOutputStream is not to write raw bytes using the
standard output stream methods, but a data output stream lets an
application write primitive Java data types to an output stream in a
portable way.
Constructors
DataInputStream(InputStream in)
Basic Java
Unit 8 Streams
DataOutputStream
void write(int b)
void writeBoolean(boolean v)
void writeByte(int v)
void writeChar(int v)
void writeInt(int v)
Writes an int to the underlying output stream as four bytes, high byte first.
void writeChars(String s)
Basic Java
Unit 8 Streams
StringReader
Class
Reader
Concept
Processing Stream
Char Array
Reader
Input
Stream
Reader
Piped
Reader
File
Reader
Basic Java
Buffered
Reader
LineNumber
Reader
Filter
Reader
Push Back
Reader
Unit 8 Streams
StringReader
Constructor
StringReader(String s)
Basic Java
Unit 8 Streams
StringReader
void close()
int read()
void reset()
Basic Java
Unit 8 Streams
String Writer
Class
Writer
Concept
Processing Stream
Char Array
Writer
Output
Stream
Writer
Piped
Writer
File
Writer
Basic Java
Buffered
Writer
Filter
Writer
Printer
Writer
Unit 8 Streams
String Writer
Constructors
StringWriter()
StringWriter(int intialSize)
Create a new string writer, using the specified initial stringbuffer size.
Basic Java
Unit 8 Streams
String Writer
StringBuffer getBuffer()
String toString()
void write(int c)
Write a string.
Basic Java
Unit 8 Streams
Question time
Please try to limit the questions to the topics discussed during the session.
Participants are encouraged to discuss other issues during the breaks.
Thank you.
Basic Java
Unit 8 Streams
Object Serialization
Basic Java
Unit 8 Streams
Serializable interface
class MyClass
implements
Serializable
{
}
Copyright 2008 Pratian Technologies
www.pratian.com
Basic Java
Unit 8 Streams
Nonserializable references
Basic Java
Unit 8 Streams
ObjectOutputStream
Output
Stream
Class
Concept
Processing Stream
Piped
Output
Stream
Filter Output
Stream
File Output
Stream
Buffered Output
Stream
Basic Java
Data Output
Stream
Object
Output
Stream
Print
Stream
Unit 8 Streams
ObjectOutputStream
public
class
ObjectOutputStream
extends
OutputStream
implements ObjectOutput , ObjectStreamConstants
Objects are serialized by using the ObjectOutputStream.
An ObjectOutputStream writes primitive data types and graphs of
Java objects to an OutputStream.
The objects can be reconstituted using a ObjectInputStream.
Persistent storage of objects can be accomplished by using a file for
the stream. If the stream is a network socket stream, the objects can
be reconstituted on another host or in another process.
The class of each serializable object is encoded including the class
name and signature of the class, the values of the object's fields and
arrays, and the closure of any other objects referenced from the initial
objects.
Basic Java
Unit 8 Streams
ObjectOutputStream
Constructors
protected ObjectOutputStream()
ObjectOutputStream(OutputStream out)
Creates an ObjectOutputStream that writes to the
specified OutputStream.
The writeObject method is responsible for writing the state of the object for its
particular class so that the corresponding readObject method can restore it.
Basic Java
Unit 8 Streams
ObjectInputStream
Class
Input
Stream
Concept
Processing Stream
Piped Input
Stream
Filter Input
Stream
File Input
Stream
Buffered Input
Stream
Basic Java
Data Input
Stream
Object Input
Stream
Push Back
Input
Stream
Unit 8 Streams
ObjectInputStream
Basic Java
Unit 8 Streams
ObjectInputStream
Constructors
protected ObjectInputStream()
ObjectInputStream(InputStream in)
Object readObject()
Basic Java
Unit 8 Streams
Question time
Please try to limit the questions to the topics discussed during the session.
Participants are encouraged to discuss other issues during the breaks.
Thank you.
Basic Java
Unit 8 Streams
Opening a stream
Open a stream
Make a connection to an external place
External place indicates the data external to your program that you
want to get from or put into
Once the connection is made, you forget about the external place and
just use the stream
FileInputStream in = new
FileInputStream(sourceFileName);
Basic Java
Unit 8 Streams
Using a stream
Some streams can be used only for input, others only for
output, still others for both
Using a stream means doing input from it or output to it
But its not usually that simple--you need to manipulate
the data in some way as it comes in or goes out
int val = in.read();
while(val != -1) {
out.write(val);
val = in.read();
}
Basic Java
Unit 8 Streams
Basic Java