How to Read All Text Files in a Folder in Java
There are many ways to read a text file in java. Let's look at java read text file different methods 1 by i.
Java read text file
There are many ways to read a text file in java. A text file is fabricated of characters, so we can use Reader classes. In that location are some utility classes as well to read a text file in java.
- Coffee read text file using Files class
- Read text file in coffee using
FileReader
- Coffee read text file using BufferedReader
- Using Scanner class to read text file in java
Now let's expect at examples showing how to read a text file in java using these classes.
Java read text file using java.nio.file.Files
We can use Files form to read all the contents of a file into a byte array. Files form also has a method to read all lines to a list of cord. Files course is introduced in Java 7 and it's proficient if yous desire to load all the file contents. Y'all should use this method only when you are working on small-scale files and you demand all the file contents in retentiveness.
Cord fileName = "/Users/pankaj/source.txt"; Path path = Paths.go(fileName); byte[] bytes = Files.readAllBytes(path); List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
Read text file in coffee using coffee.io.FileReader
Yous can use FileReader to get the BufferedReader and so read files line past line. FileReader doesn't support encoding and works with the system default encoding, so it's not a very efficient way of reading a text file in java.
Cord fileName = "/Users/pankaj/source.txt"; File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); Cord line; while((line = br.readLine()) != zippo){ //process the line Organization.out.println(line); }
Java read text file using java.io.BufferedReader
BufferedReader is good if you want to read file line by line and process on them. Information technology's expert for processing the large file and it supports encoding too.
BufferedReader is synchronized, so read operations on a BufferedReader can safely be done from multiple threads. BufferedReader default buffer size is 8KB.
String fileName = "/Users/pankaj/source.txt"; File file = new File(fileName); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, cs); BufferedReader br = new BufferedReader(isr); String line; while((line = br.readLine()) != null){ //process the line System.out.println(line); } br.close();
Using scanner to read text file in java
If you want to read file line by line or based on some java regular expression, Scanner is the class to use.
Scanner breaks its input into tokens using a delimiter design, which by default matches whitespace. The resulting tokens may then be converted into values of unlike types using the various next methods. The scanner class is not synchronized and hence not thread safety.
Path path = Paths.get(fileName); Scanner scanner = new Scanner(path); System.out.println("Read text file using Scanner"); //read line past line while(scanner.hasNextLine()){ //process each line String line = scanner.nextLine(); Organisation.out.println(line); } scanner.close();
Java Read File Example
Here is the instance form showing how to read a text file in java. The case methods are using Scanner, Files, BufferedReader with Encoding support and FileReader.
bundle com.journaldev.files; import java.io.BufferedReader; import java.io.File; import coffee.io.FileInputStream; import coffee.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import coffee.nio.file.Path; import coffee.nio.file.Paths; import java.util.Listing; import java.util.Scanner; public class JavaReadFile { public static void chief(String[] args) throws IOException { String fileName = "/Users/pankaj/source.txt"; //using Coffee 7 Files class to process modest files, go complete file data readUsingFiles(fileName); //using Scanner class for large files, to read line by line readUsingScanner(fileName); //read using BufferedReader, to read line past line readUsingBufferedReader(fileName); readUsingBufferedReaderJava7(fileName, StandardCharsets.UTF_8); readUsingBufferedReader(fileName, StandardCharsets.UTF_8); //read using FileReader, no encoding support, non efficient readUsingFileReader(fileName); } individual static void readUsingFileReader(String fileName) throws IOException { File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; System.out.println("Reading text file using FileReader"); while((line = br.readLine()) != cipher){ //procedure the line System.out.println(line); } br.close(); fr.close(); } individual static void readUsingBufferedReader(String fileName, Charset cs) throws IOException { File file = new File(fileName); FileInputStream fis = new FileInputStream(file); InputStreamReader isr = new InputStreamReader(fis, cs); BufferedReader br = new BufferedReader(isr); String line; Arrangement.out.println("Read text file using InputStreamReader"); while((line = br.readLine()) != nil){ //process the line Organisation.out.println(line); } br.close(); } individual static void readUsingBufferedReaderJava7(String fileName, Charset cs) throws IOException { Path path = Paths.get(fileName); BufferedReader br = Files.newBufferedReader(path, cs); String line; System.out.println("Read text file using BufferedReader Coffee 7 improvement"); while((line = br.readLine()) != null){ //process the line Arrangement.out.println(line); } br.shut(); } private static void readUsingBufferedReader(String fileName) throws IOException { File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; System.out.println("Read text file using BufferedReader"); while((line = br.readLine()) != null){ //procedure the line System.out.println(line); } //close resources br.close(); fr.close(); } individual static void readUsingScanner(String fileName) throws IOException { Path path = Paths.go(fileName); Scanner scanner = new Scanner(path); System.out.println("Read text file using Scanner"); //read line by line while(scanner.hasNextLine()){ //process each line String line = scanner.nextLine(); System.out.println(line); } scanner.shut(); } private static void readUsingFiles(String fileName) throws IOException { Path path = Paths.get(fileName); //read file to byte assortment byte[] bytes = Files.readAllBytes(path); System.out.println("Read text file using Files class"); //read file to String listing @SuppressWarnings("unused") List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8); Arrangement.out.println(new String(bytes)); } }
The pick of using a Scanner or BufferedReader or Files to read file depends on your project requirements. For case, if you are only logging the file, yous can use Files and BufferedReader. If you are looking to parse the file based on a delimiter, you lot should use Scanner course.
Earlier I end this tutorial, I want to mention about RandomAccessFile
. Nosotros can use this to read text file in java.
RandomAccessFile file = new RandomAccessFile("/Users/pankaj/Downloads/myfile.txt", "r"); String str; while ((str = file.readLine()) != aught) { System.out.println(str); } file.shut();
That's all for java read text file example programs.
Source: https://www.journaldev.com/867/java-read-text-file
0 Response to "How to Read All Text Files in a Folder in Java"
Publicar un comentario