In JAVA programming, there are two types of applications or programs
1. volatile or non-persistent program
2. non-volatile or persistent program.
Volatile or non-persistent program
A volatile program is one whose result is always stored in main memory of the computer
Eg: RAM
Non-volatile or persistent program
A non-volatile program is one whose result is stored in secondary storage devices i.e., hard disk
In order to store the data permanently in the form of files we must use or import a package
called java.io.*
There are 2 types of operations:
1. read mode
2. write mode
Types of streams in JAVA:
In JAVA we have two types of streams they are
1. byte streams and
2. char streams
File Hierarchy
Byte streams are those in which the data will be transferred one byte at a time between
primary memory to secondary memory
InputStream class:
This is an abstract class. This class is used to open the file in ‘read’ mode
methods:
1. public int read ();
2. public int length (); // total size of the file
3. public int available (); // available number of bytes only
4. public void close ();
OutputStream class
It is also an abstract class. This class is used to open the file in ‘write’ mode
1. public void write (int);
2. public int length ();
3. public void available ();
4. public void close ();
FileInputStream class
This is the concrete subclass of all InputStream class. This class is always used to open the file in read mode
Constructor:
FileInputStream (String fname) throws FileNotFoundException
FileOutputStream class:
This is the concrete sub-class of all OutputStream classes. This class is always used for
opening the file in write mode is nothing but creating an object of FileOutputStream class.
Constructors:
1. FileOutputStream (String fname);
2. FileOutputStream (String fname, boolean flag);
1) create a file that should contain 1 to 10 numbers
import java.io.*;
class Fos
{
public static void main (String [] args)
{
try
{
String fname=args [0];
FileOutputStream fos=new FileOutputStream (fname, true);// mean append mode
for (int i=1; i<=10; i++)
{
fos.write (i);
}
fos.close ();
}
catch (IOException ioe)
{
System.out.println (ioe);
}
catch (ArrayIndexOutOfBoundsException aiobe)
{
System.out.println ("PLEASE PASS THE FILE NAME..!");
}
System.out.println ("DATA WRITTEN..!");
}
};
2. read the data from the file which is created above
import java.io.*;
class Fis
{
public static void main(String[] args)
{
try
{
String fname=args [0];
FileInputStream fis=new FileInputStream (fname);
int i;
while ((i=fis.read ())!=-1)
{
System.out.println (i);
}
fis.close ();
}
catch (FileNotFoundException fnfe)
{
System.out.println ("FILE DOES NOT EXIST..!");
}
catch (IOException ioe)
{
System.out.println (ioe);
}
catch (ArrayIndexOutOfBoundsException aiobe)
{
System.out.println ("PLEASE PASS THE FILE NAME..!");
}
}
};
DataInputStream class
it is used for two purposes. They are
1. reading the data from input device
2. reading the data from remote machine
Constructors:
DataInputStream (InputStream);
Instance methods:
1. public byte readByte ();
2. public char readChar ();
3. public short readShort ();
4. public int readInt ();
5. public long readLong ();
6. public float readFloat ();
7. public double readDouble ();
8. public boolean readBoolean ();
9. public String readLine ();
DataOutputStream class:
This is used for displaying the data onto the console and also used for writing the data to remote machine.
Constructor:
DataOutputStream (OutputStream);
For example:
DataOutputStream dos=new DataOutputStream (System.out);
Instance methods:
Old methods:
1. public void writeByte (byte);
2. public void writeChar (char);
3. public void writeShort (short);
4. public void writeInt (int);
5. public void writeLong (long);
6. public void writeFloat (float);
7. public void writeDouble (double);
8. public void writeBoolean (boolean);
9. public void writeLine (String);
New methods:
1. public void write (byte);
2. public void write (char);
3. public void write (short);
4. public void write (int);
5. public void write (long);
6. public void write (float);
7. public void write (double);
8. public void write (boolean);
9. public void write (String);
Write a JAVA program to read two numbers from keyboard and display their product (using older methods)?
Answer:
import java.io.*;
class DataRead
{
public static void main (String [] args)
{
try
{
DataInputStream dis=new DataInputStream (System.in);
System.out.println ("Enter first number : ");
String s1=dis.readLine ();
System.out.println ("Enter second number : ");
String s2=dis.readLine ();
int n1=Integer.parseInt (s1);
int n2=Integer.parseInt (s2);
int n3=n1*n2;
System.out.println ("Product = "+n3);
}
catch (FileNotFoundException fnfe)
{
System.out.println ("FILE DOES NOT EXISTS");
}
catch (IOException ioe)
{
System.out.println (ioe);
}
catch (NumberFormatException nfe)
{
System.out.println ("PASS INTEGER VALUES ONLY");
}
}
};
No comments:
Post a Comment