How to Read Input From File in Java
There are many ways to read a text file in java. Allow'due south look at coffee read text file unlike methods one by 1.
Java read text file
There are many ways to read a text file in java. A text file is made of characters, and then we can use Reader classes. There are some utility classes likewise to read a text file in coffee.
- Java read text file using Files class
- Read text file in java using
FileReader - Java read text file using BufferedReader
- Using Scanner class to read text file in java
Now let's look at examples showing how to read a text file in coffee using these classes.
Coffee read text file using java.nio.file.Files
Nosotros can utilise Files class to read all the contents of a file into a byte assortment. Files class also has a method to read all lines to a list of cord. Files course is introduced in Java 7 and it'southward good if y'all want to load all the file contents. You should use this method only when you are working on small files and you demand all the file contents in memory.
String fileName = "/Users/pankaj/source.txt"; Path path = Paths.become(fileName); byte[] bytes = Files.readAllBytes(path); List<Cord> allLines = Files.readAllLines(path, StandardCharsets.UTF_8); Read text file in java using java.io.FileReader
You can utilise FileReader to go the BufferedReader and so read files line by line. FileReader doesn't support encoding and works with the organization default encoding, and then it'southward 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()) != nada){ //process the line System.out.println(line); } Coffee read text file using java.io.BufferedReader
BufferedReader is good if you lot desire to read file line by line and procedure on them. It's skilful for processing the big file and information technology supports encoding also.
BufferedReader is synchronized, so read operations on a BufferedReader tin 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); Cord line; while((line = br.readLine()) != aught){ //process the line System.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 class to use.
Scanner breaks its input into tokens using a delimiter blueprint, which by default matches whitespace. The resulting tokens may then exist converted into values of different types using the various next methods. The scanner class is non synchronized and hence not thread safe.
Path path = Paths.get(fileName); Scanner scanner = new Scanner(path); Organisation.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.close(); Java Read File Case
Here is the example grade showing how to read a text file in java. The example methods are using Scanner, Files, BufferedReader with Encoding support and FileReader.
package com.journaldev.files; import java.io.BufferedReader; import java.io.File; import coffee.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; import coffee.nio.charset.StandardCharsets; import java.nio.file.Files; import coffee.nio.file.Path; import java.nio.file.Paths; import java.util.Listing; import java.util.Scanner; public course JavaReadFile { public static void main(String[] args) throws IOException { String fileName = "/Users/pankaj/source.txt"; //using Java seven Files class to process small-scale files, get consummate file data readUsingFiles(fileName); //using Scanner course for large files, to read line past line readUsingScanner(fileName); //read using BufferedReader, to read line by line readUsingBufferedReader(fileName); readUsingBufferedReaderJava7(fileName, StandardCharsets.UTF_8); readUsingBufferedReader(fileName, StandardCharsets.UTF_8); //read using FileReader, no encoding back up, not 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()) != null){ //process the line System.out.println(line); } br.shut(); fr.shut(); } private 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; System.out.println("Read text file using InputStreamReader"); while((line = br.readLine()) != zippo){ //process the line Arrangement.out.println(line); } br.close(); } private static void readUsingBufferedReaderJava7(String fileName, Charset cs) throws IOException { Path path = Paths.get(fileName); BufferedReader br = Files.newBufferedReader(path, cs); Cord line; Arrangement.out.println("Read text file using BufferedReader Java 7 improvement"); while((line = br.readLine()) != null){ //process the line Organisation.out.println(line); } br.close(); } individual static void readUsingBufferedReader(Cord fileName) throws IOException { File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; Arrangement.out.println("Read text file using BufferedReader"); while((line = br.readLine()) != null){ //process the line System.out.println(line); } //close resource br.close(); fr.shut(); } individual static void readUsingScanner(Cord fileName) throws IOException { Path path = Paths.get(fileName); Scanner scanner = new Scanner(path); Organisation.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.close(); } private static void readUsingFiles(String fileName) throws IOException { Path path = Paths.get(fileName); //read file to byte assortment byte[] bytes = Files.readAllBytes(path); Arrangement.out.println("Read text file using Files class"); //read file to String list @SuppressWarnings("unused") List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8); Organisation.out.println(new String(bytes)); } } The choice of using a Scanner or BufferedReader or Files to read file depends on your project requirements. For example, if you are just logging the file, you tin can use Files and BufferedReader. If you are looking to parse the file based on a delimiter, you should use Scanner class.
Earlier I end this tutorial, I want to mention virtually RandomAccessFile. We tin utilise this to read text file in coffee.
RandomAccessFile file = new RandomAccessFile("/Users/pankaj/Downloads/myfile.txt", "r"); String str; while ((str = file.readLine()) != goose egg) { Arrangement.out.println(str); } file.close(); That'due south all for java read text file example programs.
midgettepeack1953.blogspot.com
Source: https://www.journaldev.com/867/java-read-text-file
0 Response to "How to Read Input From File in Java"
Postar um comentário