Java Program That Reads a Text File
There are many ways to read a text file in coffee. Let's look at java read text file different methods i past 1.
Coffee read text file
There are many ways to read a text file in java. A text file is made of characters, so we can employ Reader classes. There are some utility classes as well to read a text file in java.
- Java read text file using Files course
- Read text file in java using
FileReader
- Java read text file using BufferedReader
- Using Scanner class to read text file in java
Now allow's await at examples showing how to read a text file in java using these classes.
Java read text file using java.nio.file.Files
Nosotros can utilize Files class to read all the contents of a file into a byte array. Files class also has a method to read all lines to a list of string. Files class is introduced in Coffee seven and it's adept if you want to load all the file contents. You should use this method only when you are working on modest files and yous need all the file contents in memory.
String 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 java using java.io.FileReader
Yous tin use FileReader to become the BufferedReader and so read files line by line. FileReader doesn't back up encoding and works with the system default encoding, so it'due south not a very efficient way of reading a text file in java.
String fileName = "/Users/pankaj/source.txt"; File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; while((line = br.readLine()) != null){ //procedure the line Arrangement.out.println(line); }
Coffee read text file using java.io.BufferedReader
BufferedReader is proficient if you want to read file line by line and procedure on them. It's skillful for processing the large file and information technology supports encoding also.
BufferedReader is synchronized, then read operations on a BufferedReader can safely exist 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){ //procedure the line Organisation.out.println(line); } br.close();
Using scanner to read text file in java
If you desire to read file line by line or based on some java regular expression, Scanner is the course to use.
Scanner breaks its input into tokens using a delimiter pattern, which past default matches whitespace. The resulting tokens may so be converted into values of different types using the diverse next methods. The scanner class is not synchronized and hence not thread safe.
Path path = Paths.get(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(); Organization.out.println(line); } scanner.close();
Java Read File Case
Here is the instance class showing how to read a text file in coffee. The example methods are using Scanner, Files, BufferedReader with Encoding back up and FileReader.
bundle com.journaldev.files; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import coffee.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; import coffee.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Scanner; public class JavaReadFile { public static void main(String[] args) throws IOException { Cord fileName = "/Users/pankaj/source.txt"; //using Java 7 Files class to procedure small files, get complete file data readUsingFiles(fileName); //using Scanner grade for large files, to read line past 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, not efficient readUsingFileReader(fileName); } private static void readUsingFileReader(Cord 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()) != null){ //procedure the line Organisation.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); Cord line; System.out.println("Read text file using InputStreamReader"); while((line = br.readLine()) != null){ //process the line Arrangement.out.println(line); } br.close(); } individual static void readUsingBufferedReaderJava7(String fileName, Charset cs) throws IOException { Path path = Paths.go(fileName); BufferedReader br = Files.newBufferedReader(path, cs); String line; Arrangement.out.println("Read text file using BufferedReader Java 7 improvement"); while((line = br.readLine()) != zero){ //process the line System.out.println(line); } br.close(); } private static void readUsingBufferedReader(String fileName) throws IOException { File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); Cord line; System.out.println("Read text file using BufferedReader"); while((line = br.readLine()) != null){ //procedure the line Arrangement.out.println(line); } //close resources br.close(); fr.shut(); } private static void readUsingScanner(String fileName) throws IOException { Path path = Paths.go(fileName); Scanner scanner = new Scanner(path); Organisation.out.println("Read text file using Scanner"); //read line by line while(scanner.hasNextLine()){ //process each line Cord 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 array byte[] bytes = Files.readAllBytes(path); System.out.println("Read text file using Files class"); //read file to String list @SuppressWarnings("unused") Listing<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8); System.out.println(new String(bytes)); } }
The pick of using a Scanner or BufferedReader or Files to read file depends on your projection requirements. For example, if yous are just logging the file, yous tin use Files and BufferedReader. If you are looking to parse the file based on a delimiter, you should use Scanner class.
Before I end this tutorial, I want to mention about RandomAccessFile
. We can use this to read text file in java.
RandomAccessFile file = new RandomAccessFile("/Users/pankaj/Downloads/myfile.txt", "r"); Cord str; while ((str = file.readLine()) != naught) { Organisation.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
Post a Comment for "Java Program That Reads a Text File"