0% found this document useful (0 votes)
4 views2 pages

Java File Read and Write Example

The document contains two Java programs: one for reading from a file and another for writing to a file. The first program reads lines from 'D:\output.txt' and prints them to the console, while the second program writes the string 'hello' to the same file. Both programs handle exceptions during file operations.

Uploaded by

urmigadhavivie
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Java File Read and Write Example

The document contains two Java programs: one for reading from a file and another for writing to a file. The first program reads lines from 'D:\output.txt' and prints them to the console, while the second program writes the string 'hello' to the same file. Both programs handle exceptions during file operations.

Uploaded by

urmigadhavivie
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Aim : read the file data

import [Link].*;

public class fileread {

public static void main(String args[]) throws IOException {


try
{
File f=new File("D:\\[Link]");
FileReader fr=new FileReader(f);
BufferedReader w=new BufferedReader(fr);
String line;
while((line=[Link]())!=null)
{
[Link](line);

}
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}
Aim – write into file

import [Link].*;

public class filewrite {

public static void main(String args[]) throws IOException {


try
{
File f=new File("D:\\[Link]");
FileWriter fw=new FileWriter("D:\\[Link]");
BufferedWriter w=new BufferedWriter(fw);
[Link]("hello");
[Link]();
}
catch(Exception e)
{
[Link](e);
}
}
}

You might also like