-3

Lets say I have a string = "hello". how do i open a text file and check if hello exists in that text file?

contents of the text file:

hello:man:yeah

i tried using the code below. Is it file reader only reads the first line? i need it to check all lines to see if hello exists, and then if it does, take "man" from it.

try {
    BufferedReader in = new BufferedReader(new FileReader("hello.txt"));
    String str;
    while ((str = in.readLine()) != null) {
        System.out.println(str);
    }
} catch (IOException e) {
    System.out.println("Error.");
}
RohitWagh
  • 1,999
  • 3
  • 22
  • 43
John Marston
  • 11,549
  • 7
  • 23
  • 18
  • 1
    `String myArray[] = str.split(":");` There are many methods in the java [String](http://docs.oracle.com/javase/6/docs/api/java/lang/String.html) class for these types of things. – Brian Roach Jan 11 '12 at 14:46
  • 2
    Your file contains just one line... so readline while read that line and than leave the loop because the second time in.readline returns null – Hons Jan 11 '12 at 14:47
  • Your use of `BufferedReader` appears correct. Do you not see a line-by-line output of "hello.txt"? – Sergey Kalinichenko Jan 11 '12 at 14:47
  • my text file has more than one line, im only showing one here. is there a better way to say check a if a string "hello" is in the text file? – John Marston Jan 11 '12 at 14:50

3 Answers3

4

If hello:man:yeah is one line in your file, then your code is working right. readLine() will read a line until a newline is found (one line in this case).

If you just want to see if it's in the file, then you could do something like this:

 String str;
 boolean found = false;
 while ((str = in.readLine()) != null) {
       if(str != null && !found){
         found = str.contains("hello") ? true : false;
       }
    }

If you need to do a whole word search, you'll need to use a regular expression. Surrounding your search text with \b will do the whole word search. Here's a snippet (Note, StringUtils comes from Apache Commons Lang):

    List<String> tokens = new ArrayList<String>();
    tokens.add("hello");

    String patternString = "\\b(" + StringUtils.join(tokens, "|") + ")\\b";
    Pattern pattern = Pattern.compile(patternString);
    Matcher matcher = pattern.matcher(text);

    while (matcher.find()) {
        System.out.println(matcher.group(1));
    }

Of course, if you don't have multiple tokens, you can just do this:

String patternString = "\\bhello\\b";
Dave
  • 4,050
  • 6
  • 30
  • 35
  • hi thanks. using String.contains, even if the input is "hell" it will return true. i need it to be exact. – John Marston Jan 11 '12 at 14:58
  • I lifted the stuff on string matching from here: http://stackoverflow.com/questions/5091057/how-to-find-a-whole-word-in-a-string-in-java. – Dave Jan 11 '12 at 15:15
1

Use the String.contains method on each line. Each line is processed in the while loop.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
ynka
  • 1,457
  • 1
  • 11
  • 27
1

Use String.indexOf() or String.contains() method.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186