import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class FileReadWrite { public static void printDoc(String doc, String name) throws IOException{ FileWriter fw = new FileWriter(new File(name)); fw.append(doc); fw.close(); } public static String readDoc(String name) throws IOException{ BufferedReader scanner = new BufferedReader(new FileReader(name)); String res = ""; try { String line = scanner.readLine(); while (line != null){ res = res + line + "\n"; line = scanner.readLine(); } } finally { //ensure the underlying stream is always closed //this only has any effect if the item passed to the Scanner //constructor implements Closeable (which it does in this case). scanner.close(); } return res; } }