Write a program to read and write data in a file using java. 

Program Code: 
(i)
import java.io.*; 
class Test
public static void main(String args[])
try
FileOutputstream fout=new FileOutputStream("abc.txt"); 
String s="Ravi Kumar is a good player"; 
byte b[]=s.getBytes();
//converting string into byte array 
fout.write(b); 
fout.close(); 
System.out.println("File created ..."); 
}
catch(Exception e)
{System.out.println(e);

(ii)
import java.io.*; 
class SimpleRead
public static void main(String args[])
try
FileInputStream fin=new FileInputStream("abc.txt"); 
int i=0; 
while((i=fin.read())!=-1)
System.out.println((char)i); 
fin.close(); 
}
catch(Exception e)
{
system.out.println(e);

Output: 
Ravi Kumar is a good player
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: