Java io package - IT Knowledge Base

Download Report

Transcript Java io package - IT Knowledge Base

Java io package
Stream
Java i/o performs through Streams.
 A Stream is an abstraction that either produces or
consumes information.
 A stream is linked to a physical device by java i/o
system.
 An input stream can abstract to many type of inputs like
disk file, keyboard, socket, etc.
 An output stream can abstract to many type of outputs
like console, a disk file or network connection.
 Java implements streams within class hierarchies defined
in java.io package.

Byte Streams
Java 2 defines two types of streams byte
and character.
 Byte streams provide means to handle
input and output of bytes.
 Character streams provide the same for
characters.
 At lowest level all I/O is byte-oriented.

Byte Stream Classes
At the top 2 abstract classes InputStream &
OutputStream are defined.
 Each having several subclasses that handle
differences between various devices such as
disk, files, network connections and even
memory buffers.
 To use stream classes, must import java.io
 The abstract classes InputStream &
OutputStream classes define several methods
that other stream classes implement e.g. read()
and write() to read and write bytes of data.














BufferedInputStream
BufferedOutputStream
ByteArrayInputStream
ByteArrayOutputStream
DataInputStream
DataOutputStream
FileInputStream
FileOutputStream
FilterInputStream
FilterOutputStream
InputStream
OutputStream
RandomAccessFile
Buffered input stream
Buffered output stream
contains methods for reading java
standard data types
implements InputStream
implements OutputStream
Abstract class describes input stream
supports random access file I/O
Character Stream I/O classes
At the top two abstract classes Reader and
Writer classes are defined.
 They handle Unicode character stream.
 Unicode?
 Reader and Writer define methods that
the other stream classes implements e.g.
read() and write() to read and write
characters.

















BufferedReader
BufferedWriter
CharArrayReader
CharArrayWriter
FileReader
FileWriter
FilterReader
FilterWriter
InputStreamReader
LineNumberReader
OutputStreamWriter
PrintWriter
Reader
StringReader
StringWriter
Writer
buffered input character stream
buffered output character stream
Input stream that translates bytes to char
input stream that counts lines
Output stream that translate char to bytes
Output stream that contains print() and println()
Abstract class describes character stream input
Input stream that reads from a string
Output stream that writes to a string
Abstract class describes character stream
output
Predefined streams










System class defined in java.lang package which is
automatically available in all java programs.
It also contains three stream variables in,out,err.
These variable are declared public and static.
System.out refers to standard output stream.
By default it is console.
System.in refers to standard input stream.
By default it is keyboard.
System.err refers to standard error stream.
By default it is console.
However these streams may be redirected to any
compatible I/O devices.
System.in is an object of InputStream.
 System.out, System.err are an object of
PrintStream.
 These are byte streams.

Reading Console Input











Console input is accomplished by reading from System.in.
To obtain character based stream wrap System.in in a
BufferedReader object to create a character stream.
BufferdReader br = new BufferedReader( Reader inputReader)
Reader is the stream that is linked to instance to BufferedReader.
Reader is abstract class.
Its subclass is InputStreamReader which converts bytes to
characters.
InputStreamReader ir= new InputStreamReader(InputStream inps)
Because System.in refers to object of InputStream.
InputStreamReader ir= new InputStreamReader(System.in)
BufferedReader br = new BufferedReader( new
InputStreamReader(System.in));
Now br is character stream that is linked to console.
Reading Characters






To read a character from BufferedReader use
read().
Syntaxint read() throws IOException
Each time read is called it reads a character
from input stream and returns it as an integer
value.
It return -1 when end of stream is encountered.
Example
Example
Example
Writing Console Output
PrintStream class defined print(), println().
 It also defines write() by
void write(int byteval);
class WriteDemo
{
public static void main(String args[]) {
int b;
b = 'A';
System.out.write(b);
System.out.write('\n'); }
}

PrintWriter Class
For real world programs, the
recommended method of writing to the
console is through PrintWriter stream.
 It is a character based.
 It makes easy to internationalize your
program.
 The constructor –
PrintWriter(OutputStream os, boolean f)

Os is an object of Output stream and f
controls whether java flushes the output
stream every time a println() is called.
 For automatic f should be true.
 PrintWriter supports print() and println().

import java.io.*;
public class PrintWriterDemo
{
public static void main(String args[]) {
PrintWriter pw = new PrintWriter(System.out, true);
pw.println("This is a string");
int i = -7;
pw.println(i);
double d = 4.5e-7;
pw.println(d);
}
}
Reading and Writing Files
In java all files are byte-oriented.
 Java provides methods to read and write
bytes from and to a file.
 However, java allows to wrap a byteoriented file stream within a character
based object.

FileInputStream FileOutputStream classes
are used to create byte streams linked to
files.
 FileInputStream(String fileName) throws
FileNotFoundException
 FileOutputStream(String fileName) throws
FileNotFoundException

When you are done with a file, close it by calling close().
It is defined by both FileInputStream and
FileOutputStream as:
 void close() throws IOException
 To read from a file, read() can be used that is defined
within FileInputStream.
 int read() throws IOException
 Each time that it is called, it reads a single byte from the
file and returns the byte as an integer value.
 It returns -1 when end of file is encountered.
 To write to a file, use write() defined by
FileOutputStream
 void write(int byteval)throws IOException


It writes the byte specified by byteval to
the file.
 Example
Example
