Java IO (input/output) is used to process the input and produce the output.

Java uses the concept of stream to make IO operations fastly the java io package contains all the classes required for input and output operations we can perform find handling in java by java API(Application programming language).

Types of streams in java:

  1. System.out –Stranded output stream
  2. –Stranded input stream
  3. System.error –Standard error streams

Output stream:

Java applications uses an output stream to write a data to destinations it may be a file ,an array ,peripheral device sacket.

Input stream :

Java application uses an input stream to use data from input source , file an array ,peripheral devices or socket.

Output stream classes :

Output classes is an abstract classes it is super classes of all classes an output stream of bytes. An output stream accepts the output byte and sence by then to sub case.

S.NO Methode Discription
1Public void write (int)throws IOException.Is used to write a byte of the current output stream.
2Public void write (byte[])throws IOException.Is used to write an array of byte to the current output stream.
3Public void flush ()throws IOException.Flushers the current output stream.
4Public void close ()throws IOException.Is used to close the current output stream.

Input stream classes :

Input stream classes is an abstract classes it is the super class of all classes representing an input stream of bytes.

S.NO Methode Discription
1Public abstract int read ()throws IOException.Reads the next byte of data from the input stream it returns at the end of file.
1Public int avilable ()throws IOException.Returns as existence of the number of bytes that can.
1Public void close ()throws IOException.Is used to close the current input stream.

Using the file class

  • Creating file
  • Opening file
  • Closing file
  • Deleting file
  • Getting the name of a file
  • Getting the size of a file
  • Checking the existence of a file
  • Renaming a file
  • Checking whether the file is writable
  • Checking whether the file is readable

Input output exceptions


Creation of file

If we want to create and use a disk file, we need to decide the following about the file and its intended purpose.

  • Suitable name for the file
  • Data type to be stored
  • Purpose ( reading , writing, or updating)
  • Method of creating the file
Example :

/* copy  characters */
Close  file1
Public static void main(String args[])
/*file in file= new file(“input  data”);
File outfile =new file(“”);
File reader ins=null;
File writer ins=null;
Ins  = new  file reader  (in  file);
Outs  = new  file  writer (out  file);
Int  ch;
Ins.close ();
Catch(IOException  e)

Hansling Primitive Data Types

The basic input and output streams provide read/write methods that can only be used for reading/writing bytes or characters.

If we want to read/write the primitive data types such as integers and doubles,we can use filter classes as wrapper on existing input and output streams to filter data in the original stream.

The two filter classes used for creating data streams for handling prinitive types are DataInputStream and DataOutputStream. These classes use the concept of multiple contained in both the parent and class and the interface.

FileInputStream fis= new FileInputStream(infile);
DataInputStream dis= new DataInputStream(fis);

These statements first create the input file stream fis and then create the input data stream dis. These statements basically wrap dis on fis and use it as a filter.Similarly, the following statements create the output data stream dos and wrap it over the input file stream fos.

class Primitive{
public static void main(String args[])throws IOException
File primitive= new File ("");
FileInputStream fos= new FileInputStream("primitive");
DataInputStream dos= new DataInputStream(fos);
//Write primitive data to the "prim.dat" file 
//Read data from the "print.dat"file
fileinputStream fis= new FileInputStream(Primitive);
DataInputStream dos= new DataInputStream(fis);

News Walls

Copyrights © 2016. All Rights Reserved to Learn Computers Home, India. //Site Designed.