by Dinesh Thakur

When a program is executed, the input is read from the various sources and the output is sent to different destinations. Generally, keyboard and monitor screen are used as standard input and output device, respectively.

 
by Dinesh Thakur

One of the limitations of byte stream classes is that it can handle only 8-bit bytes and cannot work directly with Unicode characters. To overcome this limitation, character stream classes have been introduced in java.io package to match the byte stream classes. The character stream classes support 16-bit Unicode characters, performing operations on characters, character arrays, or strings, reading or writing buffer at a time. Character stream classes are divided into two stream classes namely, Reader class and Writer class.



 
by Dinesh Thakur

Byte stream classes are used to perform reading and writing of 8-bit bytes. Streams being unidirectional in nature can transfer bytes in one direction only, that is, either reading data from the source into a program or writing data from a program to the destination. Therefore, Java further divides byte stream classes into two classes, namely, InputStream class and OutputStrearn class. The subclasses of InputStrearn class contain methods to support input and the subclasses of OutputStrearn class contain output related methods.



 
by Dinesh Thakur

Serialization is the technique most commonly used for network applications and persistent storage of objects, in order to maintain the reference of a remote object.

 
by Dinesh Thakur

The java.io package provides a set of abstract classes that define and partially implement Filter streams. A Filter stream filters data as it is being read from or written to the stream. The two filter streams for reading and writing data are Filter input Stream and Filter Output Stream ,respectively.



 
by Dinesh Thakur

The File class explains only the file system. Java provides two special types of stream called the File input Stream and File Output Stream to read data from and write data into the file. These classes operate on the files in the native file system. File input Stream and File Output Stream are subclasses of Input Stream and Output Stream, respectively. Usually, we use File input Stream(for byte streams) or File Reader (for character streams) for reading from a file and File Output Stream(for byte streams) or File Writer (for character streams) for writing into a file.



 
by Dinesh Thakur

A file can be created using File class.

                          File;

                          f = new File (string filename);

 
by Dinesh Thakur

Streams are represented in Java as classes. The java.io package defines a collection of stream classes that support input and output (reading and writing). To use these classes, a program needs to import the java.io package, as shown below



 
by Dinesh Thakur

A flow of data is often referred to as a data stream. A stream is an ordered sequence of bytes that has a SOURCE (input stream) or a DESTINATION (output stream). In simple terms, a stream can be defined as follows.

 
by Dinesh Thakur
 
by Dinesh Thakur

The RandomAccessFile class provides a multitude of methods for reading and writing to and from files.

Although we can certainly use FileInputStream and FileOutputStream for file I/O,

RandomAccessFile provides many more features and options.

 
 
by Dinesh Thakur

A Java application can accept any number of arguments from the command line. Command-line arguments allow the user to affect the operation of an application.



 
by Dinesh Thakur

We use FilelnputStream class for reading from the file as it creates an input byte stream for reading from the file. To read the contents from the file ecomputernotes.bat, we first make an object by name say fp of FileOutputStream class and specify the file name ecomputernotes.bat as an argument to the constructor:

 
by Dinesh Thakur

The FileOutputStream class is used for creating or writing into the file. For more advanced file output, we use the RandomAccessFile class. A FileOutputStream object can be created using one of the following constructors:

 
by Dinesh Thakur

In Split Function Java Example, the string t is split into pieces (wherever space has occurred in it) with the help of split() method and all its pieces are then stored in the string array h. Then, the length of the string h is computed by length() function and the loop is invoked in reverse to display the tokens stored in string array h in reverse order.



 
by Dinesh Thakur

Properties is a subclass of Hashtable. It uses a list of values in which key and value is a String. many other Java classes  use Properties.



 
by Dinesh Thakur

java.util.StringTokenizer class split String into different token by “space” and “comma” delimiter.



 
by Dinesh Thakur

The StringTokenizer class is used to create a parser for String objects. It parses strings according to a set of delimiter characters. It implements the Enumeration interface in order to provide access to the tokens contained within a string. StringTokenizer provides three constructors:

 
by Dinesh Thakur

The Hashtable class implements a hash table data structure. A hash table indexes and stores objects in a dictionary using hash codes as the objects keys. Hash codes are integer values that identify objects. All different objects are assigned different hash values and therefore different dictionary keys.

 
by Dinesh Thakur

The BitSet class is used to create objects that maintain a set of bits. The bits are maintained as a growable set. The capacity of the bit set is increased as needed. Bit sets are used to maintain a list of flags that indicate the state of each element of a set of conditions. Flags are boolean values that are used to represent the state of an object. It is used for representing a set of true and false values.

 
by Dinesh Thakur

The Stack class provides the capability to create and use stacks within the Java programs. Stacks are· storage objects that store information by pushing it onto a stack and remove and retrieve information by popping it off the stack. Stacks implement a last-in-first-out storage capability: The last object pushed on a stack is the first object that can be retrieved from the stack. The Stack class extends the Vector class.



 
by Dinesh Thakur

The SequencelnputStream class allows you to concatenate (or combine) two or more input streams serially and make them appear as if they were a single input stream. Each input stream is read beginning from the first one until end of file (EOF) is reached, where upon it reads from the second one and so on, until end of file is reached on the last of the contained input streams.

 
by Dinesh Thakur

The java.io.PrintWriter is another very convenient I/O class that makes writing text to a file as easy as writing text to the console. The PrintWri ter class defines several constructors, the one we would be using is as follows.



 
by Dinesh Thakur

The java.io.BufferedReader and java.io.BufferedWriter classes are the character based equivalents of the byte oriented BufferedlnputStream and BufferedOutputStream classes.



 
by Dinesh Thakur

Java provides java.io.FileReader and java.io.FileWriter classes for reading from and writing characters to a file. The FileReader and FileWri ter classes can be used almost identically to the FileInputStream and FileOutputStream character stream classes.



 
by Dinesh Thakur

Java also allows you to access the contents of a file in random order i.e. data items can be read and written in any order. This is especially useful in direct access applications such as banking systems, airline reservation systems, Automatic Teller Machine (ATM) etc. where the desired information must be located immediately. Random access files (or direct access files) are analogous to arrays, where each element is accessed directly by means of its index number. Java provides java.io.RandomAccessFile class that enables you to perform random access file input and output operations as opposed to sequential file I/O offered by ByteStream and CharacterStream classes.



 
by Dinesh Thakur

Java also supports writing and reading objects to stream (such as a file). The process of reading and writing objects in a file is called object serialization. Writing an object to a file is called serializing the object and reading the object back from a file is called deserializing an object. In Java, serialization occurs automatically. Objects can be converted into a sequence of bytes, or serialized, by using the java.io.ObjectOutputStream class and they can be deserialized or converted from bytes into a structured object, by using java.io.ObjectlnputStream class.



 
by Dinesh Thakur

The BufferedOutputStream class is a subclass of FilterOutputStream that stores written data in an internal buffer (a protected byte array field named buf) until the buffer is full or the stream is explicitly flushed using the flush ()method. Then it writes the data onto the underlying output stream all at once. This class does not declare any new methods of its own, rather all the methods are inherited from the OutputStream class.

 
by Dinesh Thakur

When you need to process primitive types, Java provides DatalnputStream and DataOutputStream classes.

The DatalnputStream class is a subclass of FilterlnputStream that reads bytes from a stream and converts them into appropriate primitive type values or strings.



 
by Dinesh Thakur

In this Java Example in the main () method of the class FileIOStream, an instance fileOutput of FileOutputStream is created to write to the file mydata.dat. Then using the for loop, we write the square of the value stored in the index variable i using the write () method to the file mydata.dat. The output stream is then closed by calling the close () method.



 
by Dinesh Thakur

In order in create File object, the File class provides the following constructors.

• File (String pathname): Creates a File object associated with the file or directory specified by pathname. The pathname can contain path information as well as a file or directory name.



 
by Dinesh Thakur

File is a predefined class present in java.io package. listFiles method is a predefined method of File class.

This method is used to show the list of files present in the specified directory.
Syntax- public File[] listFiles()



 
by Dinesh Thakur

We use two classes FileInputStream, FileOutputStream i.e to read data from one file and write it to another file.

 
by Dinesh Thakur

The constructor of FileOutputStream class is overload, when we pass Boolean argument (true) in the constructor of FileOutputStream, then the new text append in the file.

 
by Dinesh Thakur

FileInputStream is a is present in java.io package and is the child class of InputStream class. It reads data in the form of bytes from a text file.



 
by Dinesh Thakur

FileOutputStream is a class present in java.io package and child class of outputstream class. It Stores data in the form of bytes. It is used to create a text file.



 
by Dinesh Thakur

The java.io package contains stream classes that support for reading and writing. To use these stream classes, we import the java.io package. For reading characters, we prefer Reader class and we use subclass BufferedReader to read data.



 
by Dinesh Thakur

RandomAccessFile provides you to read or write data from or to in a file at any location. It supports you to move around the file, and read from it and write to it, where ever you like.

 
by Dinesh Thakur

Java makes available a set of classes and methods that allows you to read from and write to files. Java supports both streams and methods to read from and write to file respectively.



 
by Dinesh Thakur

System.out.println() and System.out.print() are only used method for output on console. But there is a class PrintStream a drived class of OutputStream having a implemented method write(), it can be used to write to the console. The simplest form of write() is :

 
by Dinesh Thakur

To read a string form the Buffered Reader object, use the following version of readLine() method.

                           String readLine( ) throws IOException

 
by Dinesh Thakur

To read a character form the Buffered Reader object, use the following version of read() method. int read( ) throws IOException

The method read() will return a integer value corresponding the character from the input stream and if end of the stream come across then it will return -1.