Home Answers Viewqa Java-Beginners 8848-Java---search Java - search/find a word in a text file

 
 


Manan
Java - search/find a word in a text file
1 Answer(s)      3 years and 3 months ago
Posted in : Java Beginners

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 Pages:
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
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
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 created a text file student.txt which consists of id, name and marks of few
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
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
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
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
Compare two word file
Compare two word file  How to compare two 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... file. This program converts the text to word file by using FileOutputStream("
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
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.
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
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
Count instances of each word
Count instances of each word  I am working on a Java Project that reads a text file from the command line and outputs an alphabetical listing... going wrong...could anyone help to put me in the right direction? From my text file
Collections Exercise 4 - Word Translator
Java: Collections Exercise 4 - Word Translator 333. leftnewads2.shtml..., a simple text file might have the following structure for showing the English...: An online dictionary is needed. The user will enter a word and the program
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
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
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
Java Word Occurrence Example
) { System.out.println(e); } } } } bipul.txt Contents of the text file is as follows Java...Java Word Occurrence Example In this example we will discuss about the how... can count the occurrences of each word in a file. In this example we will use
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
open word document
open word document  how to open a word document ??   Please go through the following link: Java Read word document file The above link will provide an example that will read the document file using POI library in java
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
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
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 number to word - Java Beginners
java number to word  Can somebody please fix this to come out with the word, not the number 23? import java.util.Scanner; public class... { //----------------------------------------------------------------- // Pulls integers from a string to form a word
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
Map words to line number in text file and show occurence
Map words to line number in text file and show occurence  hi i want to Map words to line number in text file and show occurrence of word in java coding
java word counting - Java Beginners
java word counting  Hi I want a code in java that replaces a word with another when its occurred independently and ignores the case of the text... but this will change all the occurrence even if its part of another word the run
convert word document to pdf
convert word document to pdf  Hi, Could you please help me write a java code that converts the word docuemnt into PDF? I have to save the generated...? import officetools.OfficeFile; fis = new FileInputStream(new File("test.doc
how to update the text file?
how to update the text file?  if my text file contains a string and integer in each line say,: aaa 200 bbb 500 ccc 400 i need a java code to update the integer value if my input String matches with the string in file. please
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
Read text File
Read text File  Hi,How can I get line and keep in a String in Java
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
how to update the text file?
how to update the text file?  my textfile with name list.txt: Rice... in my text file, if the item i entered matches with item in text file.how can i... 150 .... ....20 lines. java code: import java.io.*; import java.util.*; class
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
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
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  
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  
Example - Replace word
Java: Example - Replace word Problem: Write a method to replaces all occurences a word in a string with another word. Assume the method signature is static String replaceWord(String original, String find
create MS Word in Java - Java Beginners
which creates MS Word in Java...create MS Word in Java  Hi, Could any one please post the code which creates the word document having different paragraphs and two tables. i have
Java word extraction program. - Java Beginners
Java word extraction program.  Need to code a public class named WordExtractor that contains a main method. Prompt the user to enter a line of text and read it in using a Scanner object. The line of text should follow the prompt
Search and return lines of text file
Search and return lines of text file  I wrote the following code, that found lines of txt file with contains certain word, but it returns only... with the word! ) Where is the problem?! Thank U :) "import java.io.*; public
pdf to text
pdf to text  how to covert pdf file (which contain table and text) into word or excel file using itext api
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
JTextArea to Word Document
: On clicking the button 'Save', the text will get saved in the word file. You... that enable us to perform read, write operations with ms word file. Here we are going to write the data into word document file through a swing component. You can
Creating MS Word document in java - Struts
Creating MS Word document in java  I have one template in MS Word which has some content. Now i want to read this doc file and wants to add some content and also i need to set header and footer in doc file. That is, reading
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