---
title: "How to find if a string is present in a text file?"  
description: "How to find if a string is present in a text file?"  
author: "Mukul Goenka"  
published: 2021-11-17  
updated: 2021-11-17  
canonical: https://www.mindstick.com/forum/156849/how-to-find-if-a-string-is-present-in-a-text-file  
category: "java"  
tags: ["java", "programming language"]  
reading_time: 1 minute  

---

# How to find if a string is present in a text file?

How to find if a [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) is [present](https://answers.mindstick.com/qa/96635/explain-about-the-various-features-present-in-ms-access) in a [text file](https://www.mindstick.com/forum/12828/converting-a-text-file-to-an-array-in-java)?

## Replies

### Reply by Ravi Vishwakarma

```
import java.util.*;import java.io.*;public class MyClass {    public static void main(String args[]) {        }        boolean findStringInFile(String filePath, String str) throws FileNotFoundException {     File file = new File(filePath);     Scanner scanner = new Scanner(file);     // read the file line by line     while (scanner.hasNextLine()) {      String line = scanner.nextLine();      if (line.contains(str)) {       scanner.close();       return true;      }     }     scanner.close();     return false;    }}
```


---

Original Source: https://www.mindstick.com/forum/156849/how-to-find-if-a-string-is-present-in-a-text-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
