File Handling (creating file and reading it simultaneously)

package file_handling;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
class FileWrite{
public static void write()throws Exception{
FileWriter writer =new FileWriter("shahi.txt");
BufferedWriter bw=new BufferedWriter(writer);
try {
bw.write(" shashi is really a good boy");
bw.newLine();
bw.write("shashi will always be a good boy");
bw.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
class FileRead{
public static void read()throws Exception{
FileReader reader =new FileReader("shahi.txt");
BufferedReader br=new BufferedReader(reader);
String sj;
try {
while((sj=br.readLine())!=null)
{
System.out.println(sj);
}
} catch (Exception e) {
}
}
}
public class File_Handling {
public static void main(String[] args)throws Exception{
FileWrite.write();
FileRead.read();
}
}

                      OUTPUT

shashi is really a good boy
shashi will always be a good boy

No comments:

Post a Comment