Java Read From Text Seperate by Paragraph
There are many means to read a text file in coffee. Allow'south look at coffee read text file different methods 1 past 1.
Java read text file
There are many ways to read a text file in java. A text file is fabricated of characters, and so we can use Reader classes. There are some utility classes likewise to read a text file in java.
- Java read text file using Files class
- Read text file in coffee using
FileReader - Java read text file using BufferedReader
- Using Scanner class to read text file in java
Now permit'southward look at examples showing how to read a text file in coffee 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 grade also has a method to read all lines to a list of string. Files form is introduced in Java seven and it'due south good if you desire to load all the file contents. You lot should apply this method only when you are working on pocket-sized files and you need all the file contents in retention.
String fileName = "/Users/pankaj/source.txt"; Path path = Paths.become(fileName); byte[] bytes = Files.readAllBytes(path); List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8); Read text file in java using java.io.FileReader
You can use FileReader to get the BufferedReader and and so read files line by line. FileReader doesn't support encoding and works with the arrangement default encoding, and then information technology's non a very efficient fashion of reading a text file in coffee.
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){ //process the line System.out.println(line); } Java read text file using java.io.BufferedReader
BufferedReader is good if you lot want to read file line past line and process on them. Information technology's good for processing the large file and it supports encoding likewise.
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){ //procedure the line Arrangement.out.println(line); } br.close(); Using scanner to read text file in coffee
If yous want to read file line past line or based on some java regular expression, Scanner is the class to utilise.
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 side by side methods. The scanner class is not synchronized and hence not thread prophylactic.
Path path = Paths.go(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 example class showing how to read a text file in coffee. The example methods are using Scanner, Files, BufferedReader with Encoding support and FileReader.
parcel com.journaldev.files; import coffee.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 coffee.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; import java.util.Scanner; public form JavaReadFile { public static void main(String[] args) throws IOException { Cord fileName = "/Users/pankaj/source.txt"; //using Coffee 7 Files grade to process pocket-sized 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 by 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(String fileName) throws IOException { File file = new File(fileName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); String line; Organization.out.println("Reading text file using FileReader"); while((line = br.readLine()) != nada){ //process the line Organization.out.println(line); } br.close(); fr.close(); } 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); Cord line; System.out.println("Read text file using InputStreamReader"); while((line = br.readLine()) != null){ //process the line Organisation.out.println(line); } br.close(); } private static void readUsingBufferedReaderJava7(Cord fileName, Charset cs) throws IOException { Path path = Paths.become(fileName); BufferedReader br = Files.newBufferedReader(path, cs); String line; System.out.println("Read text file using BufferedReader Java vii improvement"); while((line = br.readLine()) != zilch){ //procedure the line Organization.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){ //process the line System.out.println(line); } //shut resources br.close(); fr.shut(); } private static void readUsingScanner(Cord 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(); Organization.out.println(line); } scanner.close(); } 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 listing @SuppressWarnings("unused") List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8); Arrangement.out.println(new String(bytes)); } } The option of using a Scanner or BufferedReader or Files to read file depends on your projection requirements. For example, if you lot are merely logging the file, you can use Files and BufferedReader. If you are looking to parse the file based on a delimiter, you should use Scanner form.
Before I end this tutorial, I desire to mention about RandomAccessFile. We tin can use this to read text file in coffee.
RandomAccessFile file = new RandomAccessFile("/Users/pankaj/Downloads/myfile.txt", "r"); Cord str; while ((str = file.readLine()) != cipher) { Organisation.out.println(str); } file.close(); That'due south all for coffee read text file instance programs.
Source: https://www.journaldev.com/867/java-read-text-file
Enviar um comentário for "Java Read From Text Seperate by Paragraph"