public FileStatus getNextFile ( ) throws IOException { // Check if traversal is done. while ( ! doneTraversal ( ) ) { // If traversal is not done, check if the stack is not empty. while ( ! stack . isEmpty ( ) ) { // If the stack is not empty, look at the top node. Node node = stack . peek ( ) ; // Check if the top node has an element. if ( node . hasNext ( ) ) { FileStatus element = node . next ( ) ; // Is the next element a directory. if ( ! element . isDir ( ) ) { // It is a file, return it. return element ; } // Next element is a directory, push it on to the stack and // continue try { pushNewNode ( element ) ; } catch ( FileNotFoundException e ) { // Ignore and move to the next element. } continue ; } else { // Top node has no next element, pop it and continue. stack . pop ( ) ; continue ; } } // If the stack is empty, do we have more paths? while ( ! paths . isEmpty ( ) ) { FileStatus next = paths . remove ( 0 ) ; pathIdx ++ ; if ( ! next . isDir ( ) ) { return next ; } try { pushNewNode ( next ) ; } catch ( FileNotFoundException e ) { continue ; } break ; } } return null ; }
Return the next file .