HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 8 / Java 8 – Read file line by line

Java 8 – Read file line by line

In this Java 8 tutorial, learn to read a file line by line using stream api. Also learn to iterate through lines and filter the file content based on some conditions.

1. Java 8 read file – line by line

In this example, I will read the file content in lines as stream and fetch each line one at a time and check it for word "password".

Path filePath = Paths.get("c:/temp", "data.txt");

//try-with-resources
try (Stream<String> lines = Files.lines( filePath )) 
{
	lines.forEach(System.out::println);
} 
catch (IOException e) 
{
	e.printStackTrace();
}

The above program output will print the content of the file in the console line by line.

Never
store
password
except
in mind.

2. Java 8 read file – filtering stream of lines

In this example, we will read the file content as stream of lines as. Then we will filter all lines which have the word "password" in it.

Path filePath = Paths.get("c:/temp", "data.txt");

try (Stream<String> lines = Files.lines(filePath)) 
{

	 List<String> filteredLines = lines
	 				.filter(s -> s.contains("password"))
	 				.collect(Collectors.toList());
	 
	 filteredLines.forEach(System.out::println);

} 
catch (IOException e) {

	e.printStackTrace();
}

Program output.

password

We will read the content of the given file and check if any line contains word "password" then print it.

3. Java 7 – Read file using FileReader

Till java 7, we could read a file using FileReader in various ways.

private static void readLinesUsingFileReader() throws IOException 
{
    File file = new File("c:/temp/data.txt");

    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);

    String line;
    while((line = br.readLine()) != null)
    {
        if(line.contains("password")){
            System.out.println(line);
        }
    }
    br.close();
    fr.close();
}

That’s all for Java example to read a file line by line. Please put your questions on comments section.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Raj

    June 28, 2016

    Hi ,

    I want read certain word like ” Result ” . I need to print all line after result line.

    please share me code.

    Thanks in advance

  2. Saravana

    July 18, 2014

    Hi,

    Have assigned with a task to create a Data Collector using NIO and TFTP.
    From the Network Elements i have to get large files using TFTP and Java NIO.

    Could you please help me to do this.

    • Lokesh Gupta

      July 18, 2014

      You must try yourself first, if struck anywhere then let me know.

  3. Muzzammil Ayyubi

    May 6, 2014

    Awesome Lokesh…really nice article.

  4. Neha Kanojia

    May 6, 2014

    Hi,
    I am student of MTech.I have to do thesis work,where I have to read text and find the maximum probability of a particular Indian languages with Java.Suggest me how would I do that….

  5. prathap

    May 6, 2014

    hi i’m a vivid follower of

Comments are closed on this article!

Search Tutorials

Java 8 Tutorial

  • Java 8 Features
  • Java 8 forEach
  • Java 8 Stream
  • Java 8 Boxed Stream
  • Java 8 Lambda Expression
  • Java 8 Functional Interface
  • Java 8 Method Reference
  • Java 8 Default Method
  • Java 8 Optional
  • Java 8 Predicate
  • Java 8 Regex as Predicate
  • Java 8 Date Time
  • Java 8 Iterate Directory
  • Java 8 Read File
  • Java 8 Write to File
  • Java 8 WatchService
  • Java 8 String to Date
  • Java 8 Difference Between Dates
  • Java 8 Join Array
  • Java 8 Join String
  • Java 8 Exact Arithmetic
  • Java 8 Comparator
  • Java 8 Base64
  • Java 8 SecureRandom
  • Internal vs External Iteration

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)