Sunday, 15 August 2010

java - Recursion: Checking for files in Directories and reading them -



java - Recursion: Checking for files in Directories and reading them -

before speculate "this guy asking homework help", i'll go ahead , clear doubts may have , yes, related homework. however, hope not take away learning question provides me and/or reads in future.

background: we're working on recursion , our assignment asks write programme uses command arguments check directory , file contents string(that command argument). must utilize recursion this.

-i want create clear i understand assignment asking asking, how work recursively because don't it.

we did problem had find size of directory , made sense, don't how check if directory or file , based on read contents or go deeper directory until find file.

here's i've done. not sure how wrong i'm basing exclusively off of 'check size of directory' assignment did:

the folder i'm checking this: directory ---> files --inside main directory --->> 2 directories ----> files within both of directories

public class searchingforstrings {

public static void main(string[] args) { string path = "."; // default location of project file sf = new file(path); string mysteriesdirectory = args[0]; string keystring = args[1]; countlineswithstring(sf, mysteriesdirectory, keystring); } public static int countlineswithstring(file startpath, string mysteriesdirectory, string keystring) { if(!startpath.exists()) { throw new illegalargumentexception("file " + startpath + " not exist!"); } else if(startpath.isfile()) { homecoming integer.parseint(startpath.getabsolutepath()); // show file located parsing stop error flagging on part; going inquire professor if it's okay him // begin reading contents of files } else if(startpath.isdirectory()) { // our recursion take place: // going 'deeper' directory until find file //file[] subfiles = startpath.listfiles(); countlineswithstring(startpath, mysteriesdirectory, keystring); } else { throw new illegalstateexception("unknown file type: " + startpath); } }

}

in short: explain how recursion work if wanted go deeper director(y/ies)?

i'll give try. it's easier explain understand.

the recursive method, on have made decent start, might documented follows:

"for given directory: each file in directory, count lines contain given string; each directory in directory, recurse".

the recursion possible - , useful - because original target container, , 1 of types of things can contain container.

so think of counting method this:

int countlines(dir, string) // string instance variable, also, , not passed in { var countedlines = 0; each item in dir: if item file, countedlines += matchedlinesinfile(item, string); else if item dir, countedlines += countlines(item, string); else throw up; // or throw exception -- selection }

then phone call countlines exterior method original dir use, plus string.

one of things trips people recursion that, after written, doesn't seem possible can does. think through above different scenarios. if dir passed in has files , no dirs, accumulate countedlines each file in dir, , homecoming result. that's want.

if dir contain other dirs, each 1 of those, you're going phone call routine , start on contained dir. phone call accumulate countedlines each file in dir, , phone call each dir recursively downwards tree, until reaches dir has no dirs in it. , still counts lines in those, doesn't have farther downwards recurse.

at lowest level, going accumulate lines , homecoming them. second-lowest level total add together total, , start homecoming trips recursion tree.

does explain better?

java file recursion directory

No comments:

Post a Comment