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
Post A Comment:
0 comments: