6 Replies - 87972 Views - Last Post: 09 February 2010 - 01:21 PM Rate Topic: -----

#1 tlm-kram  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 12-April 08

Search inside a text file

Post icon  Posted 12 April 2008 - 06:21 AM

hello there, can anyone give me an example of how to do a search inside a text file ...
Is This A Good Question/Topic? 0
  • +

Replies To: Search inside a text file

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Search inside a text file

Posted 12 April 2008 - 08:52 AM

Hello tlm-kram,

Below is a nice simple example of opening a text file, looping through it line by line and searching for a word. Supply the string when you run the file and it would report the line number and index position of the word in the string.

Of course our example uses c:\\test.txt but you could also put that in as a parameter if you so wish. If you do that, make sure you first check its existence first before use.

// Import io so we can use file objects
import java.io.*;

public class searchfile {
	public static void main(String args[]) {
		try {
			// Open the file c:\test.txt as a buffered reader
			BufferedReader bf = new BufferedReader(new FileReader("c:\\test.txt"));
			
			// Start a line count and declare a string to hold our current line.
			int linecount = 0;
    			String line;

			// Let the user know what we are searching for
			System.out.println("Searching for " + args[0] + " in file...");

			// Loop through each line, stashing the line into our line variable.
			while (( line = bf.readLine()) != null)
			{
    				// Increment the count and find the index of the word
    				linecount++;
    				int indexfound = line.indexOf(args[0]);

    				// If greater than -1, means we found the word
    				if (indexfound > -1) {
    				     System.out.println("Word was found at position " + indexfound + " on line " + linecount);
    				}
			}

			// Close the file after done searching
			bf.close();
		}
		catch (IOException e) {
			System.out.println("IO Error Occurred: " + e.toString());
		}
	}
}



As you can read from the in-code comments we simply take in a search word or phrase and loop through the file looking for the word in each line of the file. If it is found, we report it to the user until we end the file.

This code is enclosed in a try catch block so that if any errors occur with opening or handling the file, the error will be reported to the user.

We would then run this script from the command line like so...

c:\>java searchfile "searchwordhere"

Enjoy!

"At DIC we be file searching code ninjas... makes us great hitmen too, we search and destroy!" :snap:
Was This Post Helpful? 2
  • +
  • -

#3 tlm-kram  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 12-April 08

Re: Search inside a text file

Posted 12 April 2008 - 10:31 AM

first of all thank you for your time and consideration. when i tried to your code i get the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Reader.main(Reader.java:15)
Was This Post Helpful? 0
  • +
  • -

#4 DillonSalsman  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 144
  • Joined: 30-October 07

Re: Search inside a text file

Posted 12 April 2008 - 11:03 AM

Oh man.. I love what happened to the rules of showing your own code. :|
Was This Post Helpful? 0
  • +
  • -

#5 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Search inside a text file

Posted 12 April 2008 - 01:32 PM

View PostDillonSalsman, on 12 Apr, 2008 - 11:03 AM, said:

Oh man.. I love what happened to the rules of showing your own code. :|


haha ya well sometimes Martyr2 is so eager to post some code he forgets the rules :P lol


tlm-kram the mostly likely reason ur getting this erros is because when u call the the file (java searchfile) ur not supplying an argument or you doing it from inside an editor like Jcreator, eclipse or BlueJ. in order to get this to work:

go to command prompt (pc windows key + r then type cmd) or terminal (mac applications>utilities>terminal)
and direct ur computer to the class file (cd c:\documents and settings\...)
then use the command java + name + word to search for (java searchfile hi)

the whole thing would look something like this
on PC (sorry if ur a mac user :P)
i press the little key with the flag on it then r
type cmd
hit enter
this black box comes up :P
i direct my comp to the file (on my desktop)
then run the search


--------command prompt example----------------

Microsoft Windows XP [Version 5.1.2600]
© Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\user>cd desktop

C:\Documents and Settings\user\Desktop>java searchfile d
Searching for d in file...
Word was found at position 0 on line 1

C:\Documents and Settings\user\Desktop>

--------------------------------------------------------

done

sorry if this was realllllly basic :P im bored
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Search inside a text file

Posted 12 April 2008 - 01:37 PM

gl3thr0 is absolutely right. I love it when people don't follow my obviously simple directions of providing a word on the command line.

As for showing the code, I figured it would just have been easier to provide the solution rather than struggling to explain it to someone who probably knows nothing of java as it is. From the reply with an error, I was right. ;)
Was This Post Helpful? 0
  • +
  • -

#7 jignesh272  Icon User is offline

  • New D.I.C Head

Reputation: -7
  • View blog
  • Posts: 16
  • Joined: 09-February 10

Re: Search inside a text file

Posted 09 February 2010 - 01:21 PM

hey there get how to do it with buffer reader but i am trying to read the word from the text file with Scanner class please some help ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1