Example of PrintWriter

package fileionavigationexample;
import java.io.*;
public class PrintWriterExample {
public static void main(String[] args){
File file = new File(“C:\Users\KUNDAN\Documents\AdvanceJavaNotes.txt”);
try{
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
pw.println(“Advance Java Is also Termed As Enterprise Edition ie. J2EE.”);
pw.println(“It includes Folowing Topics.”);
pw.println(“1) JDBC:Java Database Conectivity.”);
pw.println(“2) Servelet”);
pw.println(“3) JSP”);
pw.println(“4) Hibernate”);
pw.println(“5) Spring”);
pw.println(“6) Struts”);

pw.close();
fw.close();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String f = br.readLine();
while((f = br.readLine()) != null){
System.out.println(f);
}
br.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}

Example Of BufferedWriter And BufferedReader

package fileionavigationexample;
/*All Belongs to java.io package
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
*/
import java.io.*;

public class BufferedWriterAndBufferedReader extends IOException {
public static void main(String[] args) throws IOException{
File file = new File(“C:\Users\KUNDAN\Documents\JavaNotes.txt”);

FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(“Features Of Java Programming Language:”);
bw.newLine();
bw.write(“1) Java is an Object Oriented Programming Language also called OOPS Language.”);
bw.newLine();
bw.write(“2) Java language is Highly Secured. Means It provides Third party security.”);
bw.newLine();
bw.write(“3) Java language is Robust. Means 24/7 it can be accesed by the user.”);
bw.newLine();
bw.write(“4) Java language is an scale oriented programming language. Means we can restrict an application for the number of users at a time.”);

bw.close();
fw.close();

FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s = null;

while((s = br.readLine()) != null){
System.out.println(s);
}
fr.close();
br.close();

}
}

Example Of FileWriter And FileReader

package fileionavigationexample;
import java.io.*;
public class FileWriterAndFileReader {
public static void main(String[] args){
char[] in = new char[100];
int size = 0;
try{
File file = new File(“C:\Users\KUNDAN\Documents\Firsttext.txt”);
FileWriter fw = new FileWriter(file);//this is a file writer object “fw”.
fw.write(“Hello I am writing a story.”);//it is used to write character into the text file “Firsttext.txt”.
// fw.write(“Hello I am writing a story.”);
fw.close();

FileReader fr = new FileReader(file);
size = fr.read(in);//it reads the total number of bytes in a file.
for(char c:in){
System.out.print(c);
}
System.out.println(size); //return 26(21 letters + 5 spaces b/w letters)
fr.close();

}catch(IOException ex){
ex.printStackTrace();
}
}
}

/*
Output:
————-
Hello I am writing a story 26 bytes
*/