- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the Strings within a text file in Java?
Following Java program accepts a String value from the user, verifies whether a file contains the given String and prints the number of occurrences of the word too.
Example
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class FindingWordFromFile { public static void main(String args[]) throws FileNotFoundException { //Reading the word to be found from the user Scanner sc1 = new Scanner(System.in); System.out.println("Enter the word to be found"); String word = sc1.next(); boolean flag = false; int count = 0; System.out.println("Contents of the line"); //Reading the contents of the file Scanner sc2 = new Scanner(new FileInputStream("D:\sampleData.txt")); while(sc2.hasNextLine()) { String line = sc2.nextLine(); System.out.println(line); if(line.indexOf(word)!=-1) { flag = true; count = count+1; } } if(flag) { System.out.println("File contains the specified word"); System.out.println("Number of occurrences is: "+count); } else { System.out.println("File does not contain the specified word"); } } }
Output
Enter the word to be found the Contents of the line Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms. The journey commenced with a single tutorial on HTML in 2006 and elated by the response it generated, we worked our way to adding fresh tutorials to our repository which now proudly flaunts a wealth of tutorials and allied articles on topics ranging from programming languages to web designing to academics and much more. 40 million readers read 100 million pages every month Our content and resources are freely available and we prefer to keep it that way to encourage our readers acquire as many skills as they would like to. We don’t force our readers to sign up with us or submit their details either. No preconditions and no impediments. Simply Easy Learning! File contains the specified word Number of occurrences is: 3
- Related Articles
- How to find and replace within a text file using Python?\n\n\n
- How to handle the error “Text strings must be rendered within a component” in ReactNative?
- How to open a plain text file in Java?
- Reading a Text file in java
- How to Find the Shortest Words in a Text File using Python?
- How to Find the Longest Words in a Text File using Python?
- How to count the number of words in a text file using Java?
- How to count the number of lines in a text file using Java?
- How to append text to a text file in C++?
- How to find and replace the word in a text file using PowerShell?
- How to Find the Most Repeated Word in a Text File using Python?
- How to remove unique characters within strings in R?
- How to send a file and parameters within the same XMLHttpRequest
- How to find the file using Java?
- How to count the number of characters (including spaces) in a text file using Java?

Advertisements