Showing posts with label java read plain text. Show all posts
Showing posts with label java read plain text. Show all posts

Friday, 24 May 2019

Read plain text file in Java


Approach 1: Using Files.readAllBytes method
byte[] bytes = Files.readAllBytes(Paths.get(filePath));
String data = new String(bytes, "utf-8");

App.java
package com.sample.app;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {

 public static void printFileContents(String filePath) throws IOException {
  if (filePath == null || filePath.isEmpty()) {
   System.out.println("filePath can't be null or empty");
   return;
  }

  byte[] bytes = Files.readAllBytes(Paths.get(filePath));
  String data = new String(bytes, "utf-8");
  System.out.println(data);

 }

 public static void main(String args[]) throws IOException {
  printFileContents("/Users/krishna/Desktop/learn.txt");
 }
}

Approach 2: Using Files.readAllLines() method
List<String> fileContent = Files.readAllLines(Paths.get(filePath));


App.java
package com.sample.app;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class App {

 public static void printFileContents(String filePath) throws IOException {
  if (filePath == null || filePath.isEmpty()) {
   System.out.println("filePath can't be null or empty");
   return;
  }

  List<String> fileContent = Files.readAllLines(Paths.get(filePath));
  for (String content : fileContent) {
   System.out.println(content);
  }

 }

 public static void main(String args[]) throws IOException {
  printFileContents("/Users/krishna/Desktop/learn.txt");
 }
}

Approach 3: Using Files.lines
Files.lines(Paths.get(filePath)).forEach(System.out::println);


App.java
package com.sample.app;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {

 public static void printFileContents(String filePath) throws IOException {
  if (filePath == null || filePath.isEmpty()) {
   System.out.println("filePath can't be null or empty");
   return;
  }

  Files.lines(Paths.get(filePath)).forEach(System.out::println);

 }

 public static void main(String args[]) throws IOException {
  printFileContents("/Users/krishna/Desktop/learn.txt");
 }
}

Approach 4: Using BufferedReader

try (BufferedReader br = new BufferedReader(new FileReader(filePath));) {

         while ((line = br.readLine()) != null) {
                  System.out.println(line);
         }

}


App.java

package com.sample.app;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class App {

 public static void printFileContents(String filePath) throws IOException {
  String line;

  try (BufferedReader br = new BufferedReader(new FileReader(filePath));) {

   while ((line = br.readLine()) != null) {
    System.out.println(line);
   }

  }

 }

 public static void main(String args[]) throws IOException {
  printFileContents("/Users/krishna/Desktop/learn.txt");
 }
}

Approach 5: Using Scanner
try (Scanner in = new Scanner(new FileReader(filePath))) {
         while (in.hasNext()) {
                  System.out.println(in.next());
         }
}


App.java
package com.sample.app;

import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class App {

 public static void printFileContents(String filePath) throws IOException {
  try (Scanner in = new Scanner(new FileReader(filePath))) {
   while (in.hasNext()) {
    System.out.println(in.next());
   }
  }

 }

 public static void main(String args[]) throws IOException {
  printFileContents("/Users/krishna/Desktop/learn.txt");
 }
}


You may like