==================================Reading a text file line by line: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** * This program demonstrates how to read characters from a text file * using a BufferedReader for efficiency. * @author www.codejava.net * */ public class TextFileReadingExample3 { public static void main(String[] args) { try { FileReader reader = new FileReader("MyFile.txt"); BufferedReader bufferedReader = new BufferedReader(reader); String line; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } } ======================================Text File Writer: Text File Writer: - appends if file exists: import java.io.FileWriter; import java.io.IOException; /** * This program demonstrates how to write characters to a text file. * @author www.codejava.net * */ public class TextFileWritingExample1 { public static void main(String[] args) { try { FileWriter writer = new FileWriter("MyFile.txt", true); writer.write("Hello World"); writer.write("\r\n"); // write new line writer.write("Good Bye!"); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } Note that, a writer uses default character encoding of the operating system by default. It also creates a new file if not exits, or overwrites the existing one. If you want to append text to an existing file, pass a boolean flag of true to constructor of the writer class: FileWriter writer = new FileWriter("MyFile.txt", true); ======================================================Buffered File Writer: Buffered file writer (preferred ) import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; /** * This program demonstrates how to write characters to a text file * using a BufferedReader for efficiency. * @author www.codejava.net * */ public class TextFileWritingExample2 { public static void main(String[] args) { try { FileWriter writer = new FileWriter("MyFile.txt", true); BufferedWriter bufferedWriter = new BufferedWriter(writer); bufferedWriter.write("Hello World"); bufferedWriter.newLine(); bufferedWriter.write("See You Again!"); bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } ==============================================================Buffered File Writer with UTF encoding And the following example specifies specific character encoding (UTF-16) when writing to the file: import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; /** * This program demonstrates how to write characters to a text file using * a specified charset. * @author www.codejava.net * */ public class TextFileWritingExample3 { public static void main(String[] args) { try { FileOutputStream outputStream = new FileOutputStream("MyFile.txt"); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-16"); BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); bufferedWriter.write("Xin chào"); bufferedWriter.newLine(); bufferedWriter.write("Hẹn gặp lại!"); bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } ===================================================================Listing recursive directory objects Listing files in a directory recursively To list all sub files and sub directories nested in all levels, one solution is to use recursion algorithm: List files in the first level. For each file in the first level list files: If the file is a directory: Print out directory name. Repeat the step 1 and 2 with the current directory. If the file is a file: Print out file name. Continue with next file. The following method implements the above algorithm: import java.io.File; /** * * @author www.codejava.net * */ public class ListDirectoryRecurisve { public void listDirectory(String dirPath, int level) { File dir = new File(dirPath); File[] firstLevelFiles = dir.listFiles(); if (firstLevelFiles != null && firstLevelFiles.length > 0) { for (File aFile : firstLevelFiles) { for (int i = 0; i < level; i++) { System.out.print("\t"); } if (aFile.isDirectory()) { System.out.println("[" + aFile.getName() + "]"); listDirectory(aFile.getAbsolutePath(), level + 1); } else { System.out.println(aFile.getName()); } } } } public static void main(String[] args) { ListDirectoryRecurisve test = new ListDirectoryRecurisve(); String dirToList = System.getProperty("user.home") + File.separator + "Documents"; test.listDirectory(dirToList, 0); } }