09 - Java Io
09 - Java Io
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
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.
The following example shows the use of BufferedReader class as a wrapper class for
FileReader.
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.
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).