Himanshu
Himanshu

Reputation: 21

How to search for a string from a file in java?

I have written a program to download a file in java. Now i need to search for a string in that file and if the search is true it should notify through email. And furthermore have to schedule this program.. Needed Help

Upvotes: 2

Views: 8953

Answers (2)

Emil
Emil

Reputation: 13799

For searching file you can do something like this:

    Scanner s=new Scanner(System.in);
    boolean containsString=false;
    System.out.println("Enter string to search :");
    String searchWrd=s.next();
    Scanner readFile =new Scanner(new File("/../fileName"));//enter appropriate file   location
    readFile.useDelimiter("\\s+");
    while(readFile.hasNext())
        if(searchWrd.equals(readFile.next()))
        {
            containsString=true;
            break;
        }
    if(containsString)
        System.out.println("Contains searched word");
    else 
        System.out.println("Doesnot contain searched word");

and for mailing you can consider JavaMail.I didn't understand what is meant by scheduling.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503419

Well, you've got several different tasks there:

  • Loading data from a file (possibly streaming it)
  • Searching for a string within loaded data
  • Emailing the notification to the user
  • Scheduling

Focus on one problem at a time. If you get stuck on one problem, ask a specific question here, saying what you've done so far (but only related to that single problem) and where you're stuck.

Upvotes: 5

Related Questions