In  this tutorial, we will see “How to find a String in a Text File using Java 8?” check if text or string present in a file using java 8

Java 8 – How to find a ‘STRING’ in a Text File? – Simplest Examples Share on X

Java 8 1st method:

/**
 * Find a 'String' in a Text File using Java 8
 * @author Deepak Verma
 *
 */

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class Find_String_in_Text_File_Java8_Example {

	public static void main(String[] args) throws IOException {

		String searchTerm = "Techndeck";
		String filePath = "/Users/deepakverma/Documents/SampleFile.txt"; //here you need to specify the location of your text file. In our case, we have a file named 'SampleFile.txt' at the specified location and it contains a word 'Techndeck'

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

			boolean found = lines.anyMatch(line -> line.contains(searchTerm));

			if (found) {

				System.out.println("Found the search term in the file.");

			} 
			else {

				System.out.println("Did not find the search term in the file.");

			}

		}
	}

}

 

Output:

Found the search term in the file.

check if text or string present in a file using java 8

In above example, we are using the lines method of the Files class to create a stream of lines in the file, and after that it uses the anyMatch method to check if any of the lines contain the search word/term. If any line contains the search word, then the anyMatch method will return true, and the search term will be considered as found. But, If none of the lines contain the search word/term, then the anyMatch method will return false, and the search term will be considered as not found.

This can also be achieved using filter method to create a stream that only includes the lines that contain the search term, and after that, use the count method to count the number of lines that contain the search word/term.

Java 8 2nd method:

/**
 * Find a 'String' in a Text File using Java 8
 * @author Deepak Verma
 *
 */

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class Find_String_in_Text_File_Java8_Example {

	public static void main(String[] args) throws IOException {

		String searchTerm = "jlfasdjfjadsfjaldjsf";
		String filePath = "/Users/ishadeepak/Documents/SampleFile.txt"; //here you need to specify the location of your text file. In our case, we have a file named 'SampleFile.txt' at the specified location and it does not contain the word 'jlfasdjfjadsfjaldjsf'

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

			long count = lines.filter(line -> line.contains(searchTerm)).count();

			if (count>0) {

				System.out.println("Found the word in the file");

			} 
			else {

				System.out.println("Did not find the word in the file");

			}

		}
	}

}

 

Output:

Did not find the word in the file

remove whitespaces from string in java

skip method java 8 stream

Java 8 – How to find a ‘STRING’ in a Text File? – Simplest Examples Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts