Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file access 1

Status
Not open for further replies.

chessbot

Programmer
Mar 14, 2004
1,524
US
Is there a way to loop through all the files in a directory without knowing their names?

Thanks!

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
File dir = new File("the_dir");
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println(files);
}

You should use a recursive method to loop through all subdirectories ...

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks!
Is there a way to take files only of a certain extension, rather than loading them all in and checking?

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Thank you!

Is there any better way to do this than
Code:
// ...
public boolean accept(File f)
{
  return (f.getName().endsWith(".ext"));
}
// ...

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
OK good. Just wondering if RegEx would be better...

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top