• Post Reply Bookmark Topic Watch Topic
  • New Topic

to find a string in a file

 
karthik chellappan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would like 2 knw huv do we find a string in a file using bufferedinput stream....
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using BufferedInputStream is not the best way to do this. You better use a reade such as BufferedReader for this, as it is esier to use. If you use streams, you will have to manage the low level bytes etc.

However, here's a class which you could use for your problem:


------------------------
import java.io.*;

class StringSearch {

public static void main(String[] args){

//Search String
String searchText = "How";

//File to search (in same directory as .class file)
String fileName = "msg.txt";

//StringBuilder allows to create a string by concatinating
//multiple strings efficiently.
StringBuilder sb =
new StringBuilder();

try {
//Create the buffered input stream, which reads
//from a file input stream

BufferedInputStream bIn =
new BufferedInputStream(
new FileInputStream(fileName));

//Holds the position of the last byte we have read
int pos = 0;

//Holds #of available bytes in our stream
//(which is the file)
int avl = bIn.available();

//Read as long as we have something
while ( avl != 0 ) {

//Holds the bytes which we read
byte[] buffer = new byte[avl];

//Read from the file to the buffer
// starting from <pos>, <avl> bytes.
bIn.read(buffer, pos, avl);

//Update the last read byte position
pos += avl;

//Create a new string from byte[] we read
String strTemp =
new String(buffer);

//Append the string to the string builder
sb.append(strTemp);

//Get the next available set of bytes
avl = bIn.available();
}
}
catch(IOException ex) {
ex.printStackTrace();
}

//Get the concatinated string from string builder
String fileText = sb.toString();

//Displays the index location in the file for a given text.
// -1 if not found
System.out.println(
"Position in file : " +
fileText.indexOf(searchText));
}
}
-----------------------
Compile this class. Then create a file called "msg.txt", with the string "Hello How Are You". Save it in the same directory as the .class file. Then run the class.

You will get the index position of "How", which is 6.

You can fine tune this a little more to display the exact position in the file.

Its a lot more easier if you use a reader than a stream.


Good Luck.

-- Yohan
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With the use of a Reader, you can do the same thing easily like this:
(I'm using BufferedReader)

-----------------
import java.io.*;

class StringRead {

public static void main(String[] args){

String searchText = "How";
String fileName = "msg.txt";
StringBuilder sb =
new StringBuilder();

try {
BufferedReader reader =
new BufferedReader(
new FileReader(fileName));

//Reads until the end-of-file met
while (reader.ready()) {
//Read line-by-line directly
sb.append(reader.readLine());
}

}
catch(IOException ex) {
ex.printStackTrace();
}

String fileText = sb.toString();

System.out.println(
"Position in file : " +
fileText.indexOf(searchText));

}
}
---------------------
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that available() and ready() methods used in those examples both tell you whether you can read right now without blocking. They don't tell you whether you've reached end of file. To be sure you read to end, read bytes until you get a length of -1 or read lines until you get a null line.

For text files, work with the BufferedReader. The typical loop looks like:

This syntax may look odd the first time. The assignment statement "line=..." is also an expression with a value, and the value is whatever get assigned to line. We compare null to that to test for end of file.

You could also keep a line counter going here to report the position by line and column. You can test for the string you want to find within that loop and avoid making one giant string that contains the whole file. It might be possible for a really large file to use up all your memory doing that!
 
Yohan Liyanage
Ranch Hand
Posts: 132
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan...
 
karthik chellappan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you both for your responses....
i have come with another logic which goes like this....


import java.io.*;
class file
{
public static void main(String arg[])throws Exception
{
FileInputStream fis=new FileInputStream("a.txt");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s;
char ch;
int i,j,exit=0,flag=0;
boolean x=true;
System.out.println("Enter the string to be found");
s=br.readLine();
while(x)
{
j=0;
for(i=0;i<s.length();i++)
{
ch=((char)fis.read());
if(ch==-1)
{
exit=1;
System.out.println(exit+"for");
break;
}
if(ch==s.charAt(i))
{
j++;
}
}
if(exit==1)
{
System.out.println(exit+"while");
break;
}
if(j==s.length())
{
flag=1;
System.out.println("Success");
break;
}
}
if(flag==1)
System.out.println("The string is found");
else
System.out.println("The string is not found");
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!