0% found this document useful (0 votes)
25 views

09 - Java Io

The document introduces Java's java.io package and its classes for input/output (I/O) operations. It discusses character streams, byte streams, the File class for representing files, and classes like FileReader, FileWriter, BufferedReader, and PrintWriter for reading from and writing to files. It also covers serialization using ObjectInputStream and ObjectOutputStream, and how inheritance affects serialization. Examples are provided for creating and reading/writing files, wrapping I/O classes, and serializing/deserializing objects.

Uploaded by

Prasad S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

09 - Java Io

The document introduces Java's java.io package and its classes for input/output (I/O) operations. It discusses character streams, byte streams, the File class for representing files, and classes like FileReader, FileWriter, BufferedReader, and PrintWriter for reading from and writing to files. It also covers serialization using ObjectInputStream and ObjectOutputStream, and how inheritance affects serialization. Examples are provided for creating and reading/writing files, wrapping I/O classes, and serializing/deserializing objects.

Uploaded by

Prasad S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

JAVA

Introduction to java.io package


The Java Input/Output (I/O) is a part of java.io package. The java.io package contains a
relatively large number of classes that support input and output operations. The classes in the
package are primarily abstract classes and stream-oriented that define methods and subclasses
which allow bytes to be read from and written to files or other input and output sources.
There are two types of streams: Character streams and Byte streams
Character Streams: The basic unit for this type of stream is a Unicode character. These streams
are the preferred way of handling Strings and text because they support most international
character sets and languages. These streams extend the Reader and Writer classes.
Byte Streams: The basic unit of this type of stream is a byte. Byte streams are subclasses of
InputStream and OutputStream.

File Class

Java File class represents the files and directory pathnames in an abstract manner. This class is
used for creation of files and directories, file searching, file deletion etc. The File object
represents the actual file/directory on the disk. There are following constructors to create a File
object:

File(File parent, String child);creates a new File instance from a parent abstract pathname and
a child pathname string
File(String pathname): creates a new File instance by converting the given pathname string
into an abstract pathname.
File(URI uri):creates a new File instance by converting the given file: URI into an abstract
pathname.
The File class isn't used to actually read or writedata; it's used to work at a higher level, making
new empty files, searching forfiles, deleting files, making directories, and working with paths.
FileReaderThis class is used to read character files. Its read() methods are fairly low-level,
allowing you to read single characters, the whole stream of characters, or a fixed number of
IPSR Solutions Ltd www.ipsr.edu.in 1
JAVA

characters. FileReaders are usually wrapped by higher-level objects such as BufferedReaders,


which improve performance and provide more convenient ways to work with the data.
BufferedReaderThis class is used to make lower-level Reader classes like FileReader more
efficient and easier to use. Compared to FileReaders, BufferedReaders read relatively large
chunks of data from a file at once, and keep this data in a buffer. When you ask for the next
character or line of data, it is retrieved from the buffer, which minimizes the number of times
that time-intensive, file read operations are performed. In addition, BufferedReader provides
more convenient methods such as readLine(), that allow you to get the next line of characters
from a file.
FileWriterThis class is used to write to character files. Its write()methods allow you to write
character(s) or Strings to a file. FileWriters are usually wrapped by higher-level Writer objects
such as BufferedWriters or
PrintWriters, which provide better performance and higher-level, more flexible methods to
write data.
BufferedWriterThis class is used to make lower-level classes like FileWriters more efficient
and easier to use. Compared to FileWriters,BufferedWriters write relatively large chunks of
data to a file at once, minimizing the number of times that slow, file writing operations are
performed. The BufferedWriter class also provides a newLine()method to create platform-
specific line separators automatically.
PrintWriterThis class has been enhanced significantly in Java 5. Because of newly created
methods and constructors (like building a PrintWriter with a File or a String), you might find
that you can use PrintWriter in places where you previously needed a Writer to be wrapped with
a FileWriter and/or a BufferedWriter. New methods like format(), printf(), and append() make
PrintWriters very flexible and powerful.
ConsoleThis new, Java 6 convenience class provides methods to read input from the console
and write formatted output to the console.
InputStreamclass is used for reading the data such as a byte and array of bytes from an input
source. An input source can be a file, a string, or memory that may contain the data. It is an
IPSR Solutions Ltd www.ipsr.edu.in 2
JAVA

abstract class that defines the programming interface for all input streams that are inherited
from it. An input stream is automatically opened when you create it. You cans explicitly close a
stream with the close( ) method, or let it be closed implicitly when the object is found as a
garbage.
OutputStream:The OutputStream class is a sibling to InputStream that is used for writing byte
and array of bytes to an output source. Similar to input sources, an output source can be
anything such as a file, a string, or memory containing the data. Like an input stream, an output
stream is automatically opened when you create it. You can explicitly close an output stream
with the close( ) method, or let it be closed implicitly when the object is garbage collected.
FileInputStream: It is used to read bytes from a file.
FileOutputStream: It is used to write bytes to a file.
Creating Files Using Class File
Objects of type File are used to represent the actual files (but not the data in thefiles) or
directories that exist on a computer's physical disk.

If you compile and run this program, when you look at the contents ofyour current directory,
you'll discover absolutely no indication of a file calledfileWrite1.txt. When you make a new
instance of the class File, you're not yetmaking an actual file, you're just creating a filename.
Once you have a File object, thereare several ways to make an actual file.

IPSR Solutions Ltd www.ipsr.edu.in 3


JAVA

This produces the output


false
true
true
And also produces an empty file in your current directory. If you run the code asecond time you
get the output
true
false
true
Using FileWriter and FileReader
In practice, you probably won't use the FileWriter and FileReader classes without wrapping

IPSR Solutions Ltd www.ipsr.edu.in 4


JAVA

The code produces the output:


18 IPSR
Solutions
Ltd
Combining I/O classes
Combining I/O classes is sometimes called wrapping and sometimescalled chaining.
The following example shows the use of PrintWriter class as a wrapper class for FileWriter.

The following example shows the use of BufferedReader class as a wrapper class for
FileReader.

IPSR Solutions Ltd www.ipsr.edu.in 5


JAVA

Serialization
Java provides a mechanism, called object serialization where an object can be represented as a
sequence of bytes that includes the object's data as well as information about the object's type
and the types of data stored in the object.After a serialized object has been written into a file, it
can be read from the file and deserialized that is, the type information and bytes that represent
the object and its data can be used to recreate the object in memory.
Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the
methods for serializing and deserializing an object.
Working with ObjectOutputStream and ObjectInputStream
The magic of basic serialization happens with just two methods: one to serialize objectsand
write them to a stream, and a second to read the stream and deserialize objects.
ObjectOutputStream.writeObject() // serialize and write
ObjectInputStream.readObject() // read and deserialize
The java.io.ObjectOutputStream and java.io.ObjectInputStream classes areconsidered to be
higher-level classes in the java.io package, and that means that you'll wrap them around lower-
level classes, such asjava.io.FileOutputStream and java.io.FileInputStream.

IPSR Solutions Ltd www.ipsr.edu.in 6


JAVA

The output is:


before serialization : Tom Engineer 25000
after deserialization : Tom Engineer 25000

IPSR Solutions Ltd www.ipsr.edu.in 7


JAVA

If you mark an instance variable transient, it will not be serialized even thought the rest of the
object's state will be. You can supplement a class's automatic serialization process by
implementing the writeObject() and readObject() methods. If you do this, embedding calls to
defaultWriteObject() and defaultReadObject(), respectively, will handle the part of serialization
that happens normally.
How Inheritance Affects Serialization
If a superclass is Serializable, then according to normal Java interfacerules, all subclasses of
that class automatically implement Serializable implicitly.If a superclass doesn't implement
Serializable, then when a subclass objectis deserialized, the superclass constructor will be
invoked, along with its superconstructor(s).

IPSR Solutions Ltd www.ipsr.edu.in 8


JAVA

The output is:


before: Fido 35
after: Fido 42

IPSR Solutions Ltd www.ipsr.edu.in 9

You might also like