Please disable your adblock and script blockers to view this page

Search this blog

Thursday 22 November 2018

Read Text File in Java using Scanner Class

 Previously I have posted about reading text file using FileInputStream and BufferedReader. This post shows how to read text files in java using the Scanner class.


This is a sample text file


and this is the java code to read text files using the Scanner class

  1. package client;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.util.Scanner;
  5. public class ReadFileScn {
  6. public ReadFileScn() {
  7. super();
  8. }
  9. public static void main(String[] args) {
  10. // Absolute path of file that you want to read
  11. File f = new File("D:/sampleFile.txt");
  12. Scanner s = null;
  13. try {
  14. s = new Scanner(f);
  15. } catch (FileNotFoundException e) {
  16. }
  17. //Iterate over file content
  18. while (s.hasNextLine())
  19. System.out.println(s.nextLine());
  20. }
  21. }

Execute this java code

Output is


Cheers 🙂 Happy Learning

Read Text File in Java using BufferedReader

 Previously I have posted about reading text file using FileInputStream and this post shows how to read text files in java using BufferedReader class.



This is a sample text file


and this is the java code to read a text file using BufferedReader class

  1. package client;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. public class ReadFileBfr {
  8. public ReadFileBfr() {
  9. super();
  10. }
  11. public static void main(String[] args) {
  12. // Absolute path of file that you want to read
  13. File f = new File("D:/sampleFile.txt");
  14. BufferedReader bfr=null;
  15. try {
  16. bfr = new BufferedReader(new FileReader(f));
  17. } catch (FileNotFoundException e) {
  18. }
  19. String data;
  20. //Iterate over file content
  21. try {
  22. while ((data = bfr.readLine()) != null)
  23. System.out.println(data);
  24. } catch (IOException e) {
  25. }
  26. }
  27. }

Execute this java code

Output is


Cheers ðŸ™‚ Happy Learning

Wednesday 21 November 2018

Read text file in Java using FileInputStream


Recently I have seen some questions about reading text file content on the OTN forum, though it is a very simple task still some find it confusing. That’s why here I am showing how to read text files in java using FileInputStream


This is a sample text file


and here goes the java code to read text file using FileInputStream

  1. package client;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. public class ReadFileJava {
  6. public ReadFileJava() {
  7. super();
  8. }
  9. public static void main(String[] args) {
  10. // Absolute path of file that you want to read
  11. File f = new File("D:/sampleFile.txt");
  12. FileInputStream fis = null;
  13. try {
  14. fis = new FileInputStream(f);
  15. int data;
  16. //Iterate over file content
  17. while ((data = fis.read()) != -1) {
  18. System.out.print((char) data);
  19. }
  20. } catch (IOException ioe) {
  21. ioe.printStackTrace();
  22. } finally {
  23. try {
  24. if (fis != null)
  25. fis.close();
  26. } catch (IOException ioe) {
  27. ioe.printStackTrace();
  28. }
  29. }
  30. }
  31. }

Run this code

And output is


Cheers ðŸ™‚ Happy Learning