Java IO - Character Stream
Java IO - Character Stream
11/3/2025
Java I/O
• Java I/O (Input and Output) is used to process the
input and produce the output.
• Java uses the concept of a stream to make I/O
operation fast.
• The [Link] package contains all the classes
required for input and output operations.
11/3/2025
Stream
• A stream is a continuous group of data or a
channel through which data travels from one point
to another.
Two kinds streams:
• Input stream receives data from a source into a
program.
• Output stream sends data to a destination from
the program.
11/3/2025
Stream-Data Source
11/3/2025
Stream Exception
• When a stream is read/written, others threads
are blocked.
• Exception: while reading/writing in stream,
errors may be occurs.
• IOException is thrown.
11/3/2025
Standard input/output stream
• Standard input/output stream in Java is
represented by three fields of the System class
1) [Link]: standard output stream
2) [Link]: standard input stream
3) [Link]: standard error stream
11/3/2025
Stream
2 ways of performing operations on streams,
[Link] Stream:
– Used to perform I/O of 8 bit bytes.
– Process the data byte by byte
– Provides InputStream and OutputStream class.
– Provide I/O method in bytes.
– Process 1 byte at a time
[Link] Stream:
– Used to perform I/O of 16 bit Unicode
– Text files can be processed character by character
– .Provide Reader,Writer class
– Provides I/O methods in character.
– Process 2 byte at a time
11/3/2025
[Link] Stream classes
Object
Reader Writer
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link]
[Link]
11/3/2025
[Link] Stream classes
[Link]:
– Used for reading character streams and is abstract.
– Constructor:
• protected Reader(), protected Reader(Object lock)
Reader class Method Purpose:
[Link] void close() throws IOException
– Closes the stream
[Link] read() throws IOException:
– Reads one character[0 to 65535]
[Link] read(char buf[]) throws IOException
– Reads characters into an array
11/3/2025
[Link] Stream classes
Reader class Method Purpose:
[Link] skip(long n) throws IOException
– Skips n characters
[Link] ready() throws IOException
– Determines if the stream is ready to be run
11/3/2025
[Link] Stream classes
[Link]:
• An abstract class , supports writing into
streams
• Constructor:
– protected Writer(), protected Writer(Object obj)
11/3/2025
[Link] class Methods
1. abstract void close() throws IOException
– Closes the stream
2. abstract void flush( ) throws IOException
– Flushes the stream
3. void write(char[] c) throws IOException
– Writes the specified array of characters
– c= >0-65535
4. abstract void write(char [] c, intoffset, int n)
throws IOException
– Writes the specified length of characters from offset
position in the array
11/3/2025
[Link] Stream classes
Object
Reader Writer
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link]
[Link]
11/3/2025
[Link]
11/3/2025
[Link]
import [Link].*;
public class filewriter
{
public static void main(String[] args)
{
try Before:
{ [Link]
FileWriter w = new FileWriter(“S:\\java\\[Link]");
String content = "Java Programming";
[Link](content);
[Link]();
[Link]("Done");
}
catch (Exception e)
After:
[Link]
{
[Link](e);
Java Programming
}
}
}
11/3/2025
[Link]
11/3/2025
[Link]
import [Link].*;
public class filereader
{
public static void main(String args[]) [Link]
{ Java Programming
try
{
FileReader fr=new FileReader(“S:\\java\\[Link]");
int i;
while((i=[Link]())!=-1)
[Link]((char)i);
[Link]();
}
catch (Exception e) Output:
{
Java Programming
[Link](e);
}
}
}11/3/2025
[Link] Stream classes
Object
Reader Writer
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link]
[Link]
11/3/2025
[Link] class
• Java BufferedWriter class is used to provide buffering
for Writer instances.
• It makes the performance fast.
• It inherits Writer class.
• The buffering characters are used for providing the
efficient writing of single arrays, characters, and
strings.
11/3/2025
[Link] class
import [Link].*;
public class buffwriter {
public static void main(String[] args) {
try {
FileWriter fr = new FileWriter(“S:\\java\\[Link]");
BufferedWriter b=new BufferedWriter(fr); Before:
String content = "Java Programming-2019"; [Link]
[Link](content);
[Link]();
[Link]("Done");
}
catch (Exception e) {
[Link](e); After:
} [Link]
} Java Programming-2019
}
11/3/2025
[Link] class
• Java BufferedReader class is used to read the
text from a character-based input stream.
• It makes the performance fast.
• It inherits Reader class.
11/3/2025
[Link] class
import [Link].*;
public class buffreader
{
public static void main(String args[])
{ [Link]
try Java Programming-2019
{
FileReader fr=new FileReader(“S:\\java\\[Link]");
BufferedReader b=new BufferedReader(fr);
int i;
while((i=[Link]())!=-1)
[Link]((char)i);
[Link]();
[Link]();
}
catch (Exception e)
{ Output:
[Link](e);
}
}
Java Programming-2019
}
11/3/2025