More Examples
Text File
-
A sample text file named "abc.txt".
First line from File one Second Line from File one
-
A sample text file named "xyz.txt".
First line from File two Second Line from File Two
Display Text Files
- Display all text files inside a given directory path..
import java.io.*; class DisplayTextFiles { public static void main(String[] args) throws IOException { String path = "C:/users/arjun/Desktop"; File[] filesFromPath = (new File(path)).listFiles(); for (File fileFromPath : filesFromPath) { if (fileFromPath.getName().endsWith(".txt")) { // First getName() will return the file name into string and endsWith() checks the file extension like REGEX. System.out.println(fileFromPath.getName()); // Prints all txt files in Desktop. } } } }
Merge Text Files
- Read from "abc.txt" and "xyz.txt" and merge into "finalText.txt" file.
import java.io.*; class MergeFiles{ public static void main(String[] args)throws IOException{ try{ PrintWriter pw = new PrintWriter(new File("finalText.txt")); BufferedReader br = new BufferedReader(new FileReader("abc.txt")); String line = br.readLine(); while (line != null ) { // Denotes no character pw.write(line+"\n"); line = br.readLine(); // Iteratively reads the line one by one until loop terminates. } br = new BufferedReader(new FileReader("xyz.txt")); line = br.readLine(); while (line != null) { // Denotes no character pw.write(line + "\n"); line = br.readLine(); // Iteratively reads the line one by one until loop terminates. } pw.flush(); pw.close(); br.close(); }catch(IOException error){ error.printStackTrace(); } } }
Merge Text Files Alternatively
- Read from "abc.txt" and "xyz.txt" and merge into "finalText.txt" file while writing each lines from both files alternatively..
import java.io.*; class MergeFiles{ public static void main(String[] args)throws IOException{ try{ PrintWriter pw = new PrintWriter(new File("finalText.txt")); BufferedReader brOne = new BufferedReader(new FileReader("abc.txt")); BufferedReader brTwo = new BufferedReader(new FileReader("xyz.txt")); String lineOne = brOne.readLine(); String lineTwo = brTwo.readLine(); while (lineOne != null || lineTwo != null) { // Denotes no character pw.write(lineOne+"\n"); pw.write(lineTwo+"\n"); lineOne = brOne.readLine(); // Iteratively reads the line one by one until loop terminates. lineTwo = brTwo.readLine(); } pw.flush(); pw.close(); brOne.close(); brTwo.close(); }catch(IOException error){ error.printStackTrace(); } } }
Merge Text Files From Given Path
- Read text files from a given path and merge them.
import java.io.*; class DisplayTextFiles { public static void main(String[] args) throws IOException { BufferedReader br; // Declaring a variable of BufferedReader data type so that it can be used inside loop to read every .txt files listed there. String path = "C:/users/arjun/Desktop/"; // This is pre-specified path, you can change it. PrintWriter pw = new PrintWriter(path + "mergedText.txt"); String line; // This will get the lines on text files. String location; File[] filesFromPath = (new File(path)).listFiles(); for (File fileFromPath : filesFromPath) { if (fileFromPath.getName().endsWith(".txt") && !fileFromPath.getName().equals("mergedText.txt")) { // First getName() will return the file name into string and endsWith() checks the file extension like REGEX. It excludes the mergedText.txt too. System.out.println("Reading from " + fileFromPath.getName()); // Prints progress message in console. location = path + fileFromPath.getName(); br = new BufferedReader(new FileReader(location)); pw.write("From " + fileFromPath.getName() + "\n"); // This will write the text. line = br.readLine(); // Reads a line from specified file and returns string. while (line != null) { // Loop executes until the line-read is not null. pw.println(line + "\n"); // Writes each lines into the PrintWriter variable. line = br.readLine(); } pw.write("\n"); } } System.out.println("Writing completed."); // br is not initialized at this scope, so need to close() it. pw.flush(); pw.close(); } }