Java - search/find a word in a text file

Java - search/find a word in a text file

Hello,

I would like to know how to find from a list of lets say 10 but could be more, .txt files, how to search them for a word. The word will be PASS or it can be FAIL.

If PASS I want to do nothing, if FAIL, I want to record in 1 master output file, the name of the file that has FAIL in it.

Please help.

Thank you.
Manan
View Answers

March 9, 2010 at 11:32 AM

Hi Friend,

Here is the studentRecord.txt:

1 A 90 PASS
2 B 30 FAIL
3 C 80 PASS
4 D 99 PASS
5 E 85 PASS
6 F 25 FAIL

Try the following code:

import java.io.*;
import java.util.*;

public class SearchWord{
public static void main(String[]args) throws Exception{
Scanner input=new Scanner(System.in);
System.out.print("Enter word to find: ");
String word=input.next();
if(word.equals("FAIL")){
File f = new File("studentRecord.txt");
BufferedReader freader = new BufferedReader(new FileReader(f));
String s;
while ((s = freader.readLine()) != null) {

String[] st = s.split(" ");
String id = st[0];
String name = st[1];
String marks = st[2];
String result = st[3];
if (result.equals(word)) {
File file = new File("result.txt");

FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(id+" "+name+" "+marks);
out.newLine();
out.close();
}
}
}
}
}

Hope that it will be helpful for you.
Thanks









Related Tutorials/Questions & Answers:
How to Open Text or Word File on JButton Click in Java - Java Beginners
How to Open Text or Word File on JButton Click in Java  How to Open Text or Word File on JButton Click in Java
Text field save as word file
Text field save as word file  Dear experts At run how to save set of text field contains text into single Word file. To save in our desktop computer. Solve my problem
Advertisements
stop word removal from text file
stop word removal from text file  i need java source code for stop word removal from a text file
Java search word from text file
Java search word from text file In this tutorial, you will learn how to search a word from text file and display data related to that word. Here, we have... to find details: "); String word=input.next(); File f=new File("c
Search word from text file using Java
How to Search word from text file using Java In this section, we are going to search a word from the text file. For this, we have created a swing button... the user typed his/her name, it will check the name in the text file. To check
String file in to word file in java
String file in to word file in java  How to convert a String format notepad file into Ms word file in java
String file in to word file in java
String file in to word file in java  how to convert a String format word file into Ms Word file using java? please can anyone of you help me
how to match the key word from a text file
how to match the key word from a text file  p>Hi all, I have the code to match the key word and from the text. I have input like this reader.txt... ((devstr = br.readLine()) != null) { String text = devstr; String text1
Java code to convert pdf file to word file
Java code to convert pdf file to word file  How to convert pdf file to word file using Java
Convert Text To Word
; In this example, You will learn how to convert text to word file. Here, we are going to discuss about the conversion of text to word file.  Core... for converting text to word file. This program converts the text to word file by using
Reading a text file in java
in java.io.* package for reading and writing to a file in Java. To learn more about reading text file in Java see the tutorial Read File in Java. Thanks...Reading a text file in java  What is the code for Reading a text file
Scanning a word in a TEXT document......
Scanning a word in a TEXT document......  Hi Genius persons... I'm having many resumes in my FOLDER called HARISH in D:(colon) i want to scan... by searching the JAVA and .net words present in the files..if JAVA word is present means
Scanning a word in a TEXT document......
Scanning a word in a TEXT document......  Hi Genius persons... I'm having many resumes in my FOLDER called HARISH in D:(colon) i want to scan... by searching the JAVA and .net words present in the files..if JAVA word is present means
inserting text into text file using java application
inserting text into text file using java application  Hi, I want to insert a text or string into a text file using java application
Compare two word file
Compare two word file  How to compare two word file using Java
Merge multiple jasper file to one word Doc using java
Merge multiple jasper file to one word Doc using java   how to Merge multiple jasper file to one word Doc using java
ModuleNotFoundError: No module named 'text-word'
ModuleNotFoundError: No module named 'text-word'  Hi, My Python... 'text-word' How to remove the ModuleNotFoundError: No module named 'text... have to install padas library. You can install text-word python with following
ModuleNotFoundError: No module named 'text-word'
ModuleNotFoundError: No module named 'text-word'  Hi, My Python... 'text-word' How to remove the ModuleNotFoundError: No module named 'text... have to install padas library. You can install text-word python with following
ModuleNotFoundError: No module named 'text-word'
ModuleNotFoundError: No module named 'text-word'  Hi, My Python... 'text-word' How to remove the ModuleNotFoundError: No module named 'text... have to install padas library. You can install text-word python with following
Open Text or Word Document on JButton Click in Java - Java Beginners
Open Text or Word Document on JButton Click in Java  How to open Word document or Text File on JButton Click in java  Hi Friend, Try the following code: import java.io.*; import java.util.*; import javax.swing.
Problem reading word file
Problem reading word file  Deepak you provide me code for extarcting equation from a word file and also to write in a word file.But when I again want to read preveously created word file(created by your code) it gives an error
save text file - Java Beginners
save text file  hi i have just start programming in java.please guide me if i want to read a text file in java.then the text file is save in which directory
Adding a text file - Java Beginners
Adding a text file  Hello, I need a program that will search a text file of strings representing numbers of type int and will write the largest and the smallest numbers to the screen. The file contains nothing but strings
Java Count word occurrence and export it to excel file
Java Count word occurrence and export it to excel file Here is an example of scanning a text file in a local drive, and count the frequency of each word in the text file or you can say count the number or occurrence of each word
How to append text to an existing file in Java
How to append text to an existing file in Java  How to append text to an existing file in Java
click a word and search it from a file
click a word and search it from a file  i want to link a word with a file and search it form the file by clicking it. what could the code for this process
How to format text file? - Java Beginners
How to format text file?  I want the answer for following Query ***(Java code) How to open,read and format text file(notepad) or MS-word.../example/java/io/java-read-file-line-by-line.shtml Thanks
take variables in text file and run in java
take variables in text file and run in java  I have a text file which have variables 17 10 23 39 13 33 How to take "17"in java code
java script to replace words starting and ending with tild(~) symbol in a text area with the word highlighted yellow.
java script to replace words starting and ending with tild(~) symbol in a text area with the word highlighted yellow.  Hi, I have an issue with highlighting text in a text area. I am designing a page to send email. currently
text file
text file  Hello can I modify the program below so that all the numerical data is stored in an external text file,that is the data contained in the array list.Thank you! mport java.util.*; import java.text.*; import
Reading text from image file - Java Beginners
Reading text from image file  How Read text from image file
text file
text file  Hi can you help me I have to modify the program below so that all the data held in it is stored in an external text file.So there should... at the start of the program from a seperate external text file.Thank you! mport
print only Form Fillup Contents through Text File - Java Beginners
print only Form Fillup Contents through Text File   Hello Sir ,I have Designed Employee Details Form using java swing components Now i want to print... it with word means when i click on print then contents which is filled
searching a text string in file - Java Beginners
searching a text string in file  code to match a string in a file that is already mentioned in java code.if the string matches any part of a string that whole string is printed
how to read a text file with scanner in java
how to read a text file with scanner in java  Hi, I am looking for the example code in Java for reading text file line by line using the Scanner class. how to read a text file with scanner in java? Thanks   Hi
how to convert text file to xml file in java. - XML
how to convert text file to xml file in java.  Hi all, I m having some problem. Problem is I want to convert a text file which is having the no of record(i.e no of different line of information)to a xml file through java
Non Editable Text File in java - Java Beginners
Non Editable Text File in java  Hello Sir ,I want to Show NonEditable TextFile on JButton Click which Contains User Manual of Project ,How I can Do It. plz Help Me
Count characters from text file in Java
is a java code that count the occurrence of each character from text file. import...Count characters from text file in Java  At the "Count chracters fro mtext file in Java". I tried to run the code, but the error at the line have
Counting specific word occurences in a file
Counting specific word occurences in a file  Hello I have a log file from the proxy which consists all the browsing history with date,time,url,ip address etc as fields.Now i need to take only main part of the url like the domain
Character from text file - Java Beginners
Character from text file  Write a program that reads characters from a text file. Your program will count how many time each character appear in the text. Assume that the letters are case-sensitive. Example output
create , edit MS WORD like document file using Java Swing - Swing AWT
create , edit MS WORD like document file using Java Swing   In my program I have JTextArea. Text in JTextArea can be set to selected font, font style etc. I want this text in JTextArea to be saved to MS WORD like
Java program to read a text file and write to another file
Java program to read a text file and write to another file - Creating a simple program for reading text file and writing data into another text file... and we want to copy the content into another text file from our Java program
Convert text file to XML file in Java
Convert text file to XML file Java In this section, you will learn how to convert the text file into xml file in Java language. To do this, we have used... file. The Parser invoked startElement() method at the beginning of every element
how to store data in a text file - Java Beginners
how to store data in a text file  Hi friends, I want to know, how we can save the data in a .txt file using swings....... for example, i want to store the arraylist data of swing program into a text file.......and also i want
Text File I/O - DVD.java - Java Beginners
Text File I/O - DVD.java  NEED HELP PLEASE. The application should use a text file with one value on each line of the file. Each movie would use... starts, read the data file and populate the array. When the file ends, use
Java search text file
Java search text file In this tutorial, you will learn how to search a text... the name of a text file to search for. If the name does not have a .txt extension, display an error message. Then search the file in the current directory
Searching a word file on server in JSP - JSP-Servlet
Searching a word file on server in JSP  Seacrhing a word file on server side using JSP. I am Apache Tomcat Server. Please help me Sir
Java Read Lines from Text File and Output in Reverse order to a Different Text File
Java Read Lines from Text File and Output in Reverse order to a Different Text File  I need to read a file that was selected by the user using... to another text file. When that is done the output values of that file need
Java Word Processor
Java Word Processor  Problem: Design and implement a class called WordProcessor. This class is used to: ? hold a line of text that can be manipulated in various ways ? perform various manipulations and modify the text
writing a text into text file at particular line number
writing a text into text file at particular line number   Hi, thanks for quick response, I want to insert text at some particular line number.. after line number four my text will display in text file using java program

Ads