• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I iterate over the files in a directory one by one ?

 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Requirements - Use only standard Java API and no apache file utils for this.

Most of the answers I found on the internet either dont meet this requirement or load all file names into an array which can consume too much memory when no. of files = 20,000+
Please suggest how I can do this. Is there also a way to keep track of new files that were added during the execution of the loop in this code ?

Thanks.
 
K. Tsang
Bartender
Posts: 3610
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do check out the File I/O (NIO2) from Java 7 tutorial here
 
Knute Snortum
Sheriff
Posts: 3272
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, look at FileVistitor and its implementation SimpleFileVisitor in the Java API.
 
Paul Clapham
Sheriff
Posts: 21889
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ali Gordon wrote:Most of the answers I found on the internet either dont meet this requirement or load all file names into an array which can consume too much memory when no. of files = 20,000+


An array of 20,000 files isn't very big at all. Did you try one of those solutions and experience a problem?
 
Ali Gordon
Ranch Hand
Posts: 182
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Ali Gordon wrote:Most of the answers I found on the internet either dont meet this requirement or load all file names into an array which can consume too much memory when no. of files = 20,000+


An array of 20,000 files isn't very big at all. Did you try one of those solutions and experience a problem?


Not yet. But, I want to use as little memory as possible, while being practical.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!