FileInputeStream,FileoutputStream
Problem
Statement :
Take Student
information such as name, age, weight, height, city, phone from user and
store it in the
file using DataOutputStream and FileOutputStream and Retrive data using
DataInputStream and FileInputStream and display the result
Introduction:
The
DataInputStream is used in the context of DataOutputStream and can be used to
read primitives. Following is the constructor to create an InputStream:
InputStream in = DataInputStream(InputStream
in);
Once
you have DataInputStream object in hand, then there is a list of helper
methods, which can be used to read the stream or to do other operations on the
stream.
FileOutputStream
is used to create a file and write data into it. The stream would create a
file, if it doesn't already exist, before opening it for output. Here are two
constructors which can be used to create a FileOutputStream object. Following constructor takes a file name as a
string to create an input stream object to write the file:
OutputStream
f = new FileOutputStream("C:/java/hello")
Program
import
java.io.DataInputStream;
import
java.io.DataOutputStream;
import java.io.*;
import java.util.*;
import java.lang.*;
class exp9
{
public static void
main(String args[])throws IOException
{
FileOutputStream
fout=new FileOutputStream("abc.txt");
DataOutputStream
d=new DataOutputStream(fout);
d.writeUTF("ABC");
d.writeInt(45);
d.writeDouble(80.00);
d.close();
try
{
FileInputStream
fin=new FileInputStream("abc.txt");
DataInputStream
di=new DataInputStream(fin);
String
sname=di.readUTF();
System.out.println("Name
of student="+sname);
int
srollno=di.readInt();
System.out.println("Rollno
of student="+srollno);
Double sper=di.readDouble();
System.out.println("percentage
of student="+sper);
}
catch(Exception e)
{
System.out.println("not
found");
}
}
}