Skip to content
Fix Code Error

Getting the filenames of all files in a folder

March 13, 2021 by Code Error
Posted By: Anonymous

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

Solution

You could do it like that:

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());
  }
}

Do you want to only get JPEG files or all files?

Answered By: Anonymous

Related Articles

  • changing value of threshold value when getting error…
  • Getting "requires numeric/complex matrix/vector…
  • How can I resolve Web Component Testing error?
  • When I'm testing a web app by JUnit and Mockito I…
  • TLS 1.3 server socket with Java 11 and self-signed…
  • Unable to run Robolectric and Espresso with a…
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • ClassNotFoundException:…
  • Eclipse will not start and I haven't changed anything
  • Neither BindingResult nor plain target object for…
  • Jetpack Compose and Hilt Conflict
  • java IO Exception: Stream Closed
  • How to format a phone number in a textfield
  • Gradle error: Execution failed for task…
  • SQLException: No suitable Driver Found for…
  • Knight's tour Problem - storing the valid moves then…
  • Fastest way to iterate over all the chars in a String
  • SecurityException: Permission denied (missing…
  • Examples of GoF Design Patterns in Java's core libraries
  • How do I include certain conditions in SQL Count
  • How to parse JSON file with Spring
  • Can't access Eclipse marketplace
  • Requested bean is currently in creation: Is there an…
  • android studio 0.4.2: Gradle project sync failed error
  • Callback functions in C++
  • Using enums in a spring entity
  • Mock MQRFH2 header in JUnit Testing Error [MQRFH2…
  • How to add Typescript to a Nativescript-Vue project?
  • Execution failed for task…
  • org.gradle.api.tasks.TaskExecutionException:…
  • How do I install Java on Mac OSX allowing version switching?
  • How to convert number to words in java
  • ClassNotFoundException thrown
  • Android- Error:Execution failed for task…
  • Could not install Gradle distribution from…
  • JPA Hibernate Persistence exception…
  • Android Studio 4.2.1 NullPointer Exception on startup
  • How do I iterate through the files in a directory in Java?
  • Vaadin Spring Boot - There was an exception while…
  • Configure hibernate to connect to database via JNDI…
  • Regex needed to match a proper currency format of…
  • How to convert java.util.Date to java.sql.Date?
  • Get value (String) of ArrayList(); in Java
  • When to use LinkedList over ArrayList in Java?
  • Why is 2 * (i * i) faster than 2 * i * i in Java?
  • Launching Spring application Address already in use
  • java.lang.RuntimeException: Unable to instantiate…
  • Sort table rows In Bootstrap
  • Calling Web API from MVC controller
  • Getting Dropwizard Client And Jersey/HTTP I/O Error…
  • How to fix Hibernate LazyInitializationException:…
  • "Non-resolvable parent POM: Could not transfer…
  • Error inflating class android.support.v7.widget.Toolbar?
  • java.lang.NullPointerException: Attempt to invoke…
  • Eclipse fails to open .vue files in Web Page Editor
  • What does "Could not find or load main class" mean?
  • java.lang.ClassNotFoundException: HttpServletRequest
  • Drawing a simple line graph in Java
  • Efficiency of Java "Double Brace Initialization"?
  • Adding gif image in an ImageView in android
  • Java String Array Iteration: Is there any way to…
  • Spring Boot with ElasticSearch in Groovy: WebClient…
  • How to declare an ArrayList with values?
  • Android Webview - Completely Clear the Cache
  • How to create an Array, ArrayList, Stack and Queue in Java?
  • org.springframework.beans.factory.NoSuchBeanDefiniti…
  • Spring Boot - Cannot determine embedded database…
  • Java ElasticSearch None of the configured nodes are…
  • Plugin…
  • How to print a number with commas as thousands…
  • "Keep Me Logged In" - the best approach
  • Algo not working for String Decode Ways -- ,…
  • problem with client server unix domain stream…
  • Problems using Maven and SSL behind proxy
  • UnsatisfiedDependencyException: Error creating bean…
  • What's the difference between Instant and LocalDateTime?
  • Combine and minify CSS with PHP
  • NullpointerException error while working with…
  • What is a stack trace, and how can I use it to debug…
  • Name [jdbc/mydb] is not bound in this Context
  • How to filter a RecyclerView with a SearchView
  • Gradle: Execution failed for task ':processDebugManifest'
  • list all files in the folder and also sub folders
  • Injection of autowired dependencies failed;
  • Javax.net.ssl.SSLHandshakeException:…
  • Address already in use: JVM_Bind
  • Spring schemaLocation fails when there is no…
  • Check if a path represents a file or a folder
  • Apache server keeps crashing, "caught SIGTERM,…
  • Failed to execute goal…
  • How to deserialize Arraylist items into concrete…
  • Eclipse - Run in Debug Mode fails
  • HashMap(key: String, value: ArrayList) returns an…
  • java.net.UnknownHostException: Unable to resolve…
  • Multipart File Upload Using Spring Rest Template +…
  • Palindromic numbers in Java
  • Baffling variadic templates exercise
  • Android + Pair devices via bluetooth programmatically
  • Jetty server throws idle timeout for REST calls
  • Problem while using user insert for getter

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

if…else within JSP or JSTL

Next Post:

What is the argument for printf that formats a long?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error