Home > Tutorial > Java List Files Directory Sample Code

Java List Files Directory Sample Code

This tutorial will list the contents of all files in a directory or folder. The File.list() method will gather all files in a location and save the names to a String array.


Java List Files Directory

This working Java source code creates a new File object that points to a directory or folder and will list all files in that directory or folder.

package com.jcd.java.tutorials;

import java.io.File;

public class JavaListFilesDirectory 
{

	public static void main(String[] args) 
	{
		
		//Create File Object pointing to a directory or folder
	    File jcdFile = new File("C:\\Users\\Admin\\Documents\\Workspace\\Tutorials\\src\\main\\java\\com\\jcd\\java\\tutorials");
	    
	    //Get list of files and store in our array
	    String[] jcdFiles = jcdFile.list();
	    
	    //Print out number of files
	    System.out.println("Files Found: "+jcdFiles.length);
	    
	    //Add blank line
	    System.out.println("");
	    	    
	    //Enhance loop through all files in the directory or folder
	    for(String myFile : jcdFiles) 
	    {
	    	System.out.println(myFile);
	    }

	}

}


Java List Files Directory Output

Running this Java program will list all files in the specified directory or folder as seen in the output below.

Files Found: 69

JavaAbsoluteValue.java
JavaArrayList.java
JavaArrayListCombine.java
JavaArrayListCopy.java
JavaArrayListRemove.java
JavaArrays.java
JavaBase64EncodeDecode.java
JavaCodeDepot.java
JavaCommandLineArgs.java
JavaConvertArrayList.java
JavaCountOccurrences.java
JavaCreateFile.java
JavaCustomException.java
JavaCustomThreadFactory.java
JavaDataTypes.java
JavaDeleteFile.java
JavaDividingNumbers.java
JavaEncapsulation.java
JavaEnvironmentVariables.java
JavaFileExists.java
JavaFileReader.java
JavaFileSize.java
JavaFileWriter.java
JavaGetUrlContents.java
JavaHashMap.java
JavaIfElseControlFlow.java
JavaInheritance.java
JavaLastModTime.java
JavaLinkedList.java
JavaListFilesDirectory.java
JavaLog4JLoggingSetup.java
JavaMathMaximum.java
JavaMathMinimum.java
JavaMathRounding.java
JavaPinger.java
JavaRandomNumberGenerator.java
JavaReadUserInput.java
JavaRenameFile.java
JavaSingletonClass.java
JavaSortHashMapByKey.java
JavaSortHashMapByValue.java
JavaSortListNumbers.java
JavaStringBuffer.java
JavaStringBuilder.java
JavaStringCaseConvert.java
JavaStringCompareTo.java
JavaStringContains.java
JavaStringEquals.java
JavaStringIndexOf.java
JavaStringLength.java
JavaStringReplace.java
JavaStringReverse.java
JavaStringSplit.java
JavaStringToInteger.java
JavaStringTrim.java
JavaSubString.java
JavaSwitchStatement.java
JavaSystemProperties.java
JavaTernaryOperator.java
JavaThreadExample.java
JavaThreadPool.java
JavaThreadStats.java
JavaTimestamp.java
JavaWatchService.java
JavaWatchServiceRunnable.java
JsonSimpleFileReader.java
JsonSimpleFileWriter.java
ScheduledExecutorServiceTutorial.java
Tutorials.java



Concluding Comments

This program will print out just the names of the file. If you want to read, move, edit, delete, etc one of the files, you will need to pre-append the full path. Just add the getPath() method to the front of the filename like below.

//Enhance loop through all files in the directory or folder
for(String myFile : jcdFiles) 
{
	System.out.println(jcdFile.getPath()+"\\"+myFile);
}



Java References:

Smedley
Posted by

Smedley

Smedley has been doing software development for years, which mainly docues on C and C++. In the past number of years, he has focused on Java programming.

You may also like...

Leave a Reply

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

Copyright © 2018 JavaCodeDepot