-------------------File reader-------------------------------------------------------
package samplecode;
// Reading data from a file using FileReader
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class Exp12 {
public static void main(String[] args) throws Exception {
// variable declaration
int ch;
// check if File exists or not
FileReader fr = null;
try {
fr = new FileReader("D:\\Demo.txt");
} catch (FileNotFoundException fe) {
System.out.println("File not found");
}
// read from FileReader till the end of file
while ((ch = fr.read()) != -1)
System.out.print((char) ch);
// close the file
fr.close();
}
}
------------------------File Writer----------------------------------
//Creating a text File using FileWriter
import java.io.FileWriter;
import java.io.IOException;
class Exp12
{
public static void main(String[] args) throws IOException
{
// Accept a string
String str = "File Handling in Java using "+
" FileWriter and FileReader";
// attach a file to FileWriter
FileWriter fw=new FileWriter("D:\\output.txt");
// read character wise from string and write
// into FileWriter
for (int i = 0; i < str.length(); i++)
fw.write(str.charAt(i));
System.out.println("File written successfully....");
//close the file
fw.close();
}
--------------------------------------------------------------------------
To embed this project on your website, copy the following code and paste it into your website's HTML: