Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (3.5k points)

I need to create a list with all names of the files in a folder.

For example, if I have:

000.jpg

012.jpg

013.jpg

I want to store them in a ArrayList with [000,012,013] as values.

What's the best way to do it in Java ?

PS: I'm on Mac OS X

1 Answer

0 votes
by (46k points)

You can try:

File folder = new File("your/path");

File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {

  if (listOfFiles[i].isFile()) {

    System.out.println("File " + listOfFiles[i].getName());

  } else if (listOfFiles[i].isDirectory()) {

    System.out.println("Directory " + listOfFiles[i].getName());

  }

}

Related questions

0 votes
1 answer
asked Sep 16, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Aug 11, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...