0% found this document useful (0 votes)
3 views11 pages

Java Io Basics

The document provides an overview of Java I/O basics, including the definition and handling of IOException, streams, and predefined streams such as System.in, System.out, and System.err. It explains byte streams and character streams, detailing their classes and usage for reading and writing data in Java. Additionally, it outlines the steps for reading from and writing to files using the java.io and java.nio packages.

Uploaded by

Pratyush Dixit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Java Io Basics

The document provides an overview of Java I/O basics, including the definition and handling of IOException, streams, and predefined streams such as System.in, System.out, and System.err. It explains byte streams and character streams, detailing their classes and usage for reading and writing data in Java. Additionally, it outlines the steps for reading from and writing to files using the java.io and java.nio packages.

Uploaded by

Pratyush Dixit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

JAVA I/O Basics

IOException :

 An IOException is any unexpected problem the JVM encounters while


attempting to run a program.
 When an IOException is thrown, it means that whatever is throwing the
exception (perhaps a try{}-catch block that reads data from a file) can throw an
IOException, for example if the file is not found, corrupted, etc, or when the
file is otherwise unable to be read, or any other of a list of issues that can
occur with the IO package and it's extensions.

Streams :

 Stream simply represents a sequence of data that flows from source to


destination (bytes or unicode characters) in some sort of a sequential queue.
 Java programs perform I/O through streams. A stream is an abstraction that
either produces or consumes information(read info. From an i/p stream and
write info. To an o/p stream)
 A stream is a logical connection between a java application and file so that
we can send data to store and read data to retrieve back .
 A program can manage multiple stream at a time.

The Predefined Streams :


System is a class define in java.lang package which define several aspects of run
time environment. These fields can be declared as public ,static, final.
 System.out refers to the standard output stream. By default, this is the
console.It is a type PrintStream.(writing)
 System.in refers to standard input, which is the keyboard by default. System.in
is an object of type InputStream.(reading)
 System.err refers to the standard error stream, which also is the console by
default. It is a type PrintStream.(for error or exception message)
I/O Class Hierarchy

ByteStream:

 This is used to process data byte by byte (8 bits).


 the most frequently used classes
are, FileInputStream and FileOutputStream.
 The FileInputStream is used to read from the source and FileOutputStream
is used to write to the destination .
 A byte stream is suitable for processing raw data like binary files.
 names of byte streams end with InputStream/OutputStream
 These classes are part of the java.io package.
 They are normally used for coping with non-textual statistics, studying
and writing files
 It doesn’t perform any individual encoding or decoding.It doesn’t
interpret as a character.
Types of Streams:

Input Stream
 These streams are used to read data that must be taken as an input from a
source array or file or any peripheral device. For eg., FileInputStream,
BufferedInputStream, ByteArrayInputStream etc.

 Java InputStream class is the superclass of all the io classes i.e. representing
an input stream of bytes.

 Applications that are defining a subclass of the Java InputStream class must
provide a method, that returns the next byte of input.

Declaration of Java InputStream Class


public abstract class InputStream
extends Object
implements Closeable

Output Stream:

 These streams are used to write data as outputs into an array or file or any
output peripheral device. For eg., FileOutputStream, BufferedOutputStream,
ByteArrayOutputStream etc.
 An output stream accepts output bytes and sends them to some sink .
Constructor and Description
OutputStream() : Single Constructor

Example of Byte Stream


Character Streams:
 The java.io package provides CharacterStream classes to overcome the
limitations of ByteStream classes, which can only handle the 8-bit bytes and is
not compatible to work directly with the Unicode characters.
 CharacterStream classes are used to work with 16-bit Unicode characters. They
can perform operations on characters, char arrays and Strings.
 CharacterStream classes are mainly used to read characters from the
source and write them to the destination.
 They are designed to address character based records, which include
textual records contains letter, digit,symbol and other connection.
 They perform individual encoding and decoding on characters. It convert
a particular individual statistics to and from the underlying byte
circulation the usage of a particular individual encoding such as ASCII

There are 2 types of Character stream classes:

 Reader
 Writer

Reader Class

 Reader class is used to read the 16-bit characters from the input stream.
 it is an abstract class and can't be instantiated, but there are various subclasses
that inherit the Reader class and override the methods of the Reader class.
 All methods of the Reader class throw an IOException.

Class Description

1. BufferedReader This class provides methods to read characters from the buffer.

2. CharArrayReader This class provides methods to read characters from the char array.

3. FileReader This class provides methods to read characters from the file.

4. FilterReader This class provides methods to read characters from the underlying character
input stream.

5 InputStreamReader This class provides methods to convert bytes to characters.


6 PipedReader This class provides methods to read characters from the connected piped output
stream.

7 StringReader This class provides methods to read characters from a string.

Writer class

 Writer class is used to write 16-bit Unicode characters to the output stream.
 Like Reader class, Writer class is also an abstract class that cannot be
instantiated.
 the subclasses of the Writer class are used to write the characters onto the
output stream.

SN Class Description

1 BufferedWriter This class provides methods to write characters to the buffer.

2 FileWriter This class provides methods to write characters to the file.

3 CharArrayWriter This class provides methods to write the characters to the character
array.

4 OutpuStreamWriter This class provides methods to convert from bytes to characters.

5 PipedWriter This class provides methods to write the characters to the piped output
stream.

6 StringWriter This class provides methods to write the characters to the string.
Example Of Character Stream

Reading and Writing Files In Java

Java provides two core libraries for handling files:

1. java.io package

2. java.nio package

 The java.io package is used for file handling in Java. It provides various classes for
handling files, such as File, FileReader, FileWriter, FileInputStream, and
FileOutputStream.
 The java.nio package is the newer file handling library, introduced in Java 1.4. It
provides better performance and scalability than the java.io package. It provides
classes like FileChannel, ByteBuffer, and Charset.

Reading from file

In Java, FileInputStream and FileReader class is used for reading binary


files and text files data from a files.

To read data from a file, you need to follow the following steps:

 Create a File object that represents the file you want to read.

 Create a FileReader object that reads data from the file.

 Use the read() method of the FileReader object to read data from the file.

 Close the FileReader object using the close() method.

Using BufferedReader and FileReader


FileInputStream.

Writing Files In Java

In Java, FileOutputStream and FileWriter class is used for reading binary


files and text files data to a files.

To write data to a file, you need to follow the following steps:

1. Create a File object that represents the file you want to write to.

2. Create a FileWriter object that writes data to the file.

3. Use the write() method of the FileWriter object to write data to the file.

4. Close the FileWriter object using the close() method.


Using BufferedWriter and FileWriter

FileOutputStream

You might also like