How can I get the filenames of all files in a folder which may or may not contain duplicates

0 votes

I want the names of files in an arraylist .

I have these files:

paa.jpg
maa.jpg
baa.jpg

Store them in a ArrayList with values

[paa,maa,baa]

May 3, 2018 in Java by anonymous
624 views

2 answers to this question.

0 votes

This could be done in this way:

File fol = new File("D:/folder");
File[] listFiles= fol.listFiles();
    for (int i = 0; i < listFiles.length; i++) {
      if (listFiles[i].isFile()) {
        System.out.println("File " + listFiles[i].getName());
      } else if (listFiles[i].isDirectory()) {
        System.out.println("Directory " + listFiles[i].getName());
      }
    }
answered May 3, 2018 by sharth
• 3,350 points
0 votes
List<String> results = new ArrayList<String>();


File[] files = new File("/path/to/the/directory").listFiles();
//If this pathname does not denote a directory, then listFiles() returns null. 

for (File file : files) {
    if (file.isFile()) {
        results.add(file.getName());
    }
}
answered Sep 12, 2018 by Sushmita
• 6,880 points

Related Questions In Java

0 votes
2 answers

How can I invoke a method when the method name is in the form of a given string?

You could probably use method invocation from reflection: Class<?> ...READ MORE

answered Aug 19 in Java by Sirajul
• 37,930 points
72 views
0 votes
1 answer

How can I get the current date and time in UTC or GMT in Java?

This definitely returns UTC time: as String ...READ MORE

answered Jun 7, 2018 in Java by Rishabh
• 3,540 points
8,069 views
0 votes
2 answers

What is the use of toString method in Java and how can I use it ?

Whenever you require to explore the constructor ...READ MORE

answered Aug 23, 2018 in Java by Daisy
• 8,050 points
830 views
0 votes
3 answers

How can I separate the digits of an int number in Java?

import java.util.*; public class AmstrongNumber { public static void ...READ MORE

answered Aug 6 in Java by anonymous
5,124 views
0 votes
2 answers

Scanner is skipping nextLine() after using next() or nextFoo()?

use input.nextLine(); after your nextInt() function for example:- input.nextInt(); ...READ MORE

answered May 11 in Java by Sourav Dangi
1,306 views
0 votes
1 answer

The best way to filter a Java Collection?

 this problem is solved using streams and ...READ MORE

answered May 28, 2018 in Java by parth295
• 4,640 points
79 views
0 votes
1 answer
0 votes
1 answer

What are preparedstatements? How do they differ from statements?

Statement is used for static queries like ...READ MORE

answered Jun 7, 2018 in Java by Daisy
• 8,050 points
22 views
0 votes
2 answers

Get all the permutations of a string in Java

You could use recursion to do this.  Try ...READ MORE

answered Aug 21 in Java by Sirajul
• 37,930 points
111 views
0 votes
1 answer

How can I make the return type of a method generic?

First of all, define callFriend: public <T extends ...READ MORE

answered May 18, 2018 in Java by sharth
• 3,350 points
40 views