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
578 views

2 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,310 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,820 points

Related Questions In Java

0 votes
1 answer

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

I tried doing it this way - java.lang.reflect.Method ...READ MORE

answered Apr 27, 2018 in Java by developer_1
• 3,220 points
31 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,520 points
3,287 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,020 points
156 views
0 votes
2 answers

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

You can convert a number into String ...READ MORE

answered Aug 10, 2018 in Java by Sushmita
• 6,820 points
76 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
449 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,590 points
44 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,020 points
11 views
0 votes
1 answer

Get all the permutations of a string in Java

This is the recursive way of finding a ...READ MORE

answered May 10, 2018 in Java by sharth
• 3,310 points
55 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,310 points
25 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.