Friday 18, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 21 May 2017

How to retrieve all the files from Directory using Java

String[] java.io.File.list()

Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of strings is returned, one for each file or directory in the directory. Names denoting the directory itself and the directory's parent directory are not included in the result. Each string is a file name rather than a complete path.

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

Note that the java.nio.file.Files class defines the newDirectoryStream method to open a directory and iterate over the names of the files in the directory. This may use less resources when working with very large directories, and may be more responsive when working with remote directories.

Returns:
An array of strings naming the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
Throws:
SecurityException - If a security manager exists and its SecurityManager.checkRead(String) method denies read access to the directory

ShowFiles.java

01.package com.javatutorialcorner.regex;
02. 
03.import java.io.File;
04. 
05.public class ShowFiles {
06. 
07. private static final String DIRECOTORY_PATH = "C:\\JTC\\Java\\Examples\\Directory\\File";
08. 
09. public static void main(String args[]) {
10.  retrieveFiles();
11. }
12. 
13. private static void retrieveFiles() {
14.  File directory = new File(DIRECOTORY_PATH);
15.  String[] files = directory.list();
16. 
17.  if (files == null) {
18.   System.out.println("Directory does not exist");
19.  } else {
20.   for (int i = 0; i < files.length; i++) {
21.    String filename = files[i];
22.    System.out.println(filename);
23.   }
24.  }
25. 
26. }
27.}

Output
excel.xlsx
image.bmp
test1.txt
word doc.docx


Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to change File Last modified Date using Java Following example shows how to change file last modified date using file.setLastModified() method in Java. setLastModifiedpublic boolean setLastModified(long time)  Sets the last-modified time of the file or directory … Read More
  • How to add value to all types of Collections Following example shows how to add values in all types collections in Java. Sample Program AddValueToCollection.java package com.javatutorialcorner.collection; import java.util.ArrayList; import java.util.Collection; impo… Read More
  • How to Split the String in JavaFollowing Example shows how to split the string using str.split(String pattern) method in Java. Sample Programpackage com.javatutorialcorner.javastring;public class SplitString { public static void main(String[] args) { … Read More
  • How to convert a String to Lower caseFollowing example shows how to convert a string to lower case. Sample Programpackage com.javatutorialcorner.javastring;public class ToLowerCase { public static void main(String[] args) { String input = "Java tutoria… Read More
  • How to check file existence in Java Following example shows how to check file existence using file.exists() method in Java. existspublic boolean exists() Tests whether the file or directory denoted by this abstract pathname exists. Returns:    … Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to retrieve all the files from Directory using Java Rating: 5 Reviewed By: eHowToNow