java - Recursively traverse directory structure on condition -


i never worked files in java , trying find solution traversing directory structures.

i have structure this: \uploaded\abc\uncle bob\another dir\hello-123. here hello-123 contains kind of files , may contain more sub-directories.

now start path \uploaded\abc\ start walk. after there 2 more directories, time, maximum depth walk in (after 'another dir').

goal: find directory matches input , upload files in directory (or sub directories).

example: given input hello-123 want walk through directories starting start path (maybe allow depth +2 beyond that?) , files in directory (if found), after every file processed terminate process.

any appreciated!

this looks homework i'm going answer advice rather code.

traversing file or other tree structures recursive process.

first of want think how can write recursive method. thing going act on directory, method wants passed parameter. need think return type. recursive method find , return hello-123 directory, or take action - ie want code responsible "do something".

next need consider base cases; when method decide whether take action or whether call itself.

your base cases me like;

  • found directory looking (hello-123)
  • reached allowable depth (you mentioned allowing depth of 2 below starting directory)
  • reached maximum depth (the directory has no sub-directories)

if of these base cases met want either return call stack or take action. have careful think thing might return looks like. if none of these cases met want figure out how further recurse.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -