— The java.io package contains many classes to perform simple operation like read and write data to a file.

A simple program to write to a file and read the contents is:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TextFile {

public static void main(String[] args) {
//creating a file

File f = new File("C:\\ruma\\temp.txt");
try {
// Writing to a file
FileWriter w = new FileWriter("C:\\ruma\\temp.txt");
//creating an object of FileWriter class
BufferedWriter out = new BufferedWriter(w);
//creating an object for buffered writer class to write into file fast
out.write("First Line written into the file");
out.write("Second Line written into the file");
out.flush(); //end of file

//reading from a file
FileReader r = new FileReader("C:\\ruma\\temp.txt");
BufferedReader bfr = new BufferedReader(r);
String x = " ";

while((x= bfr.readLine()) != null){
} catch (IOException e) {
// TODO Auto-generated catch block