How to Read Words From a File in Java

There are many means to read a text file in coffee. Let's expect at java read text file unlike methods one by one.

Java read text file

java read file, java read text file

In that location are many ways to read a text file in java. A text file is made of characters, so nosotros tin use Reader classes. There are some utility classes too to read a text file in java.

  1. Coffee read text file using Files form
  2. Read text file in java using FileReader
  3. Java read text file using BufferedReader
  4. Using Scanner course to read text file in java

Now permit's await at examples showing how to read a text file in java using these classes.

Coffee read text file using java.nio.file.Files

We can utilise 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 cord. Files class is introduced in Java 7 and it's good if you want to load all the file contents. You should use this method but when you are working on small files and yous demand 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

You can use FileReader to get the BufferedReader and then read files line past line. FileReader doesn't support encoding and works with the organization default encoding, so it's 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()) != aught){     //process the line     System.out.println(line); }                      

Java read text file using java.io.BufferedReader

BufferedReader is practiced if yous want to read file line past line and process on them. It'due south adept for processing the large file and it supports encoding likewise.

BufferedReader is synchronized, so read operations on a BufferedReader tin can safely be washed from multiple threads. BufferedReader default buffer size is 8KB.

                          Cord 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      Organisation.out.println(line); } br.close();                      

Using scanner to read text file in coffee

If you lot desire to read file line by line or based on some java regular expression, Scanner is the grade to use.

Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various side by side methods. The scanner grade is not synchronized and hence not thread safe.

                          Path path = Paths.become(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.close();                      

Java Read File Instance

Hither is the example class showing how to read a text file in coffee. The instance methods are using Scanner, Files, BufferedReader with Encoding support and FileReader.

                          package com.journaldev.files;  import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.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 coffee.util.List; import java.util.Scanner;  public class JavaReadFile {      public static void main(Cord[] args) throws IOException {         String fileName = "/Users/pankaj/source.txt";                  //using Java 7 Files class to process small files, go complete file data         readUsingFiles(fileName);                  //using Scanner grade 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 back up, 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;         System.out.println("Reading text file using FileReader");         while((line = br.readLine()) != null){             //process the line             System.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);         String line;         Organisation.out.println("Read text file using InputStreamReader");         while((line = br.readLine()) != null){             //process the line             Arrangement.out.println(line);         }         br.shut();              }      private static void readUsingBufferedReaderJava7(String fileName, Charset cs) throws IOException {         Path path = Paths.get(fileName);         BufferedReader br = Files.newBufferedReader(path, cs);         String line;         Organization.out.println("Read text file using BufferedReader Coffee seven improvement");         while((line = br.readLine()) != nothing){             //process 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;         Organisation.out.println("Read text file using BufferedReader");         while((line = br.readLine()) != nada){             //process the line             System.out.println(line);         }         //shut resource         br.close();         fr.shut();     }      individual static void readUsingScanner(String 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();     }      individual static void readUsingFiles(Cord fileName) throws IOException {         Path path = Paths.go(fileName);         //read file to byte array         byte[] bytes = Files.readAllBytes(path);         Arrangement.out.println("Read text file using Files form");         //read file to Cord list         @SuppressWarnings("unused") 		List<String> allLines = Files.readAllLines(path, StandardCharsets.UTF_8);         System.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 only logging the file, you lot can use Files and BufferedReader. If yous are looking to parse the file based on a delimiter, yous should use Scanner class.

Earlier I end this tutorial, I want to mention most RandomAccessFile. We 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()) != cypher) { 	Organization.out.println(str); } file.close();                      

That's all for coffee read text file example programs.

delgadowiturpred.blogspot.com

Source: https://www.journaldev.com/867/java-read-text-file

0 Response to "How to Read Words From a File in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel