How to find the longest line in a text file in Java

In this tutorial, we are going to witness how to find the longest line in a text file in Java.
If you don’t know how to do it then, you are in the right place to know your problem’s solution.

Find the longest line in a text file in Java

Before going to the code let’s figure out what is Stream class in java because in below’s code stream has been used.
Presented in Java 8, the Stream API is utilized to process assortments of articles. A stream is a succession of items that supports different techniques that can be pipelined to deliver the ideal result.

You may learn: How to remove blank lines from a text file in Java

Suppose I have a text file whose name is MyOwnDocuments and in this file, the statements that are written is as follows:-

hello everyone,
My name is Shubham raj
currently, persuing my BCA from university of petroleum and energy studies, Dehradun
and I will complete my graduation in 2020.

Now, below is the java code to find the longest line.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.*F;
 
 
public class LongestLine {
 
  private int MaxWords = 0;
  private List<String> lc = new ArrayList<String>(); // lc denotes linecount
 
  public int getCurrentMax() {
    return MaxWords;
  }
 
  public void setCurrentMaxCount(int MaxWords) {
    this.MaxWords = MaxWords;
  }
 
  public List<String> getLines() {
    return lc;
  }
 
  public void setLines(List<String> lc) {
    this.lc = lc;
  }
 
  public static void main(String a[]) {
 
    LongestLine skr = new LongestLine();
 
    skr.readMax("MyOwnDocuments.txt");
 
    print("The longest line with Maximum Word Count in a text file is:- ");
 
    List<String> lc = skr.getLines();
    for (String Line : lc) {
      println(Line);
    }
 
    println("Above line has " + skr.getCurrentMax() + " words.");
  }
 
  public void readMax(String File) {
 
    Stream<String> Stream = null;
    try {
 
      
      Stream = Files.lines(Paths.get(File));
 
    } catch (IOException exception) {
      exception.printStackTrace();
    }
 
    print("Printing file FindLongestLineFromFile.txt \n");
    Stream.forEach(line -> aLines(line));
  }
 
  private void aLines(String Line) {
    println(Line);
 
    
    int Count = (Line.split("\\s+")).length;
 
    if (Count > MaxWords) {
 
      lc.clear();
      lc.add(Line);
      MaxWords = Count;
 
    } else if (Count == MaxWords) {
      lc.add(Line);
    }
  }
  
  private static void println(String Line) {
    System.out.println(Line);
    
  }
 
  private static void print(String string) {
    System.out.print(string);
 
  }
 
}

The output of the code is:-

Printing file MyOwnDocuments.txt 
hello everyone,
My name is Shubham raj
currently, persuing my BCA from university of petroleum and energy studies, Dehradun
and I will complete my graduation in 2020.
 
The longest line with Maximum Word Count in a text file is:- currently, persuing my BCA from university of petroleum and energy studies, Dehradun

 

Leave a Reply

Your email address will not be published. Required fields are marked *