Seach

how to read and write to a file

import java.io.*;
public class file
{
public static void main(String[] s)
{
BufferedReader b;
FileReader r=null;
FileWriter w=null;
try
{
System.out.println("Enter the data to Write to a file:");
b=new BufferedReader(new InputStreamReader(System.in));
w=new FileWriter("data.txt");
w.write(b.readLine());
b.close();
w.close();
r=new FileReader("data.txt");
b=new BufferedReader(r);
System.out.println("the data stored on the data.txt");
System.out.println(b.readLine());
b.close();
r.close();
}
catch(Throwable t)
{
System.out.println("occured exception is="+t);
}
}
}

No comments: