Tutorialspoint examples

Learn java tutorial with examples for beginners online

list all file names from folder java


The list() method of File class is used to get the list of file names in the folder or directory.

Example:

package com.tutorialspointexamples;
 
import java.io.File;
 
public class ListFileNames {
  public static void main(String args[]){
	 File file = new File("D:/Test files/");
     String[] fileList = file.list();
     for(String name:fileList){
         System.out.println(name);
     }
  }
}

Output:

file1.docx
file2.docx
newTest.txt

Download this example.

Download tutorialspointexamples Android App
Copyright © 2018 Tutorialspoint examples DMCA.com Protection Status