Jump to content

Photo

Java - Directory Search by File Content


  • Please log in to reply
1 reply to this topic

#1 kurt91

kurt91

    Follower of Destiny

  • Members
  • Real Name:Kurtis
  • Location:Eastern Washington University

Posted 03 August 2016 - 01:46 AM

It's me again. I need some help with my programming homework again. This time around, the teacher didn't give us any idea how to do this, and expected us to scour the API. However, I've been looking all up and down the thing, and I can't find anything that would help me figure this out.

 

We need to write a program that accepts a path, file name, extension type, and a String to check the content. It uses all of these parameters, skipping whatever the user leaves blank, to search for a file that matches, and uses recursion to check folders inside the folder being searched currently.

 

I've gotten everything done, except for checking file content. I can't find anything on how to check inside of the file being searched for a matching sample of text. The assignment was actually due yesterday, but I held off on turning it in because I wanted to get it fully working. At this point, though, I'm about ready to give up.

 

I figured I'd ask for help for this last part, and if nobody can help me by the time I get out of class tomorrow, I'll just turn it in as-is. If anybody knows enough to help me out though, I'd really like to get the last bit working before I turn it in. (My teacher's unusually accepting when it comes to late work, as long as it's "reasonably" late.)

 

Does anybody know how to compare a text file's contents against a String that the user provides?



#2 Gleeok

Gleeok

    It's dangerous to dough alone, bake this.

  • Members
  • Real Name:Pillsbury
  • Location:Magical Land of Dough

Posted 03 August 2016 - 03:02 AM

Just google for "java string" find or contains substring.

It's basically just:
a)load file as text. (read the docs)
b)get a string representation of that text. (read the docs)
c)search for an occurrence of substring in said string. (read the docs)

psuedocode:
string str = file.Load(filename).toString();
if(str.find(someOtherString)){
//you've found a matching string in the file.
}


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users