AIM:
To write a java program using file input and output.
PROCEDURE:
1) Start the program.
2) To create a class name is subclass.
3) To create a methods in get(), put(), write(), and read().
4) The get() member function to get the input in the program.
5) The put() member function to display the variables.
6) The write() member function to create a text file using FileOutputStream. To get the input in the text file.
7) The read() member function to create the same text file name using FileInputStream. To display the variables in the file.
8) To create a main class and objects.
9) To call the member function using objects.
10) Save and execute the program.
11) Stop the program.
SOURCE CODE:
import java.io.*;
import java.lang.String;
class subclass
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name,f_name,address,age,reg_no,gender;
void get()
{
try
{
System.out.println("Enter the name:");
name=br.readLine();
System.out.println("Enter the Register no:");
reg_no=br.readLine();
System.out.println("Enter the Gender:");
gender=br.readLine();
System.out.println("Enter the father name:");
f_name=br.readLine();
System.out.println("Enter the age:");
age=br.readLine();
System.out.println("Enter the address:");
address=br.readLine();
}
catch(IOException e)
{
System.out.println(e);
}
}
void put()
{
System.out.println("Name:"+name);
System.out.println("Register no:"+reg_no);
System.out.println("Gender(M/F):"+gender);
System.out.println("Father Name:"+f_name);
System.out.println("Age:"+age);
System.out.println("Address:"+address);
}
void write()
{
try
{
FileOutputStream fout=new FileOutputStream("kanaga.txt");
DataOutputStream d1=new DataOutputStream(fout);
d1.writeChars(name+"\n");
d1.writeChars(reg_no+"\n");
d1.writeChars(gender+"\n");
d1.writeChars(f_name+"\n");
d1.writeChars(age+"\n");
d1.writeChars(address+"\n");
d1.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
void read()
{
try
{
FileInputStream fin=new FileInputStream("kanaga.txt");
DataInputStream di=new DataInputStream(fin);
boolean EOF=false;
while(fin.available()!=0)
{
name=di.readLine();
reg_no=di.readLine();
gender=di.readLine();
f_name=di.readLine();
age=di.readLine();
address=di.readLine();
put();
}
}
catch(IOException e)
{
System.out.println(e);
}
}
}
class file_example
{
public static void main(String args[])
{
subclass s=new subclass();
s.get();
s.write();
s.read();
}
}
Enter the name: Sam
Enter the Register no: 1017
Enter the Gender: male
Enter the father name: Antony
Enter the age: 22
Enter the address: trichy
Name: S a m
Register no: 1 0 1 7
Gender(M/F): m a l e
Father Name: A n to ny
Age: 2 2
Address: t r i c h y
Kanaga.txt
S a m
1 0 1 7
m a l e
A n to ny
2 2
t r i c h y
0 comments:
Post a Comment