PrintWriter
Solves problems of FileWriter and BufferedWriter
Constructors :
PrintWriter bw = new PrintWriter(String fname);
PrintWriter bw = new PrintWriter(File f);
PrintWriter bw = new PrintWriter(Writer w);
Methods
write (int ch)
write(char[] ch)
write(String s)
flush()
close()
print(char ch)...print(int i)...print(boolean b)...print(String s)
println(char ch)...println(int i)...println(boolean b)...println(String s)
Examples
Write On File
- Write some text on file named "abc.txt".
import java.io.*; class Sample { public static void main(String[] args) { try { PrintWriter pw = new PrintWriter("abc.txt"); pw.print('d'); // Writes character pw.print(100 + "\n"); // Writes integer pw.print(true + "\n"); // Writes boolean pw.print('c' + "\n"); // Writes Character pw.print("gces" + "\n"); // Writes string pw.flush(); pw.close(); } catch (IOException error) { error.printStackTrace(); } } }