- Two Java examples to show you how to list files in a directory: For Java 8, Files.walk Before Java 8, create a recursive loop to list all files.
- The list method of the Java File class is used to list all the files and subdirectories present inside a directory. It returns all the files and directories as a string array. It returns all the files and directories as a string array.
- Java Add File To Directory
- Java File Directory Example
- Java Files Create File In Directory Folder
- Java Get Directory Files
This cookbook covers working with files and directories from Clojure,using functions in the
clojure.java.io
namespace as well as parts ofthe JDK via interoperability.A JAVA file is used by a Java compiler to create a Java class files (.CLASS), which is usually a binary file and not human readable. If the source code file contains multiple classes, each one is compiled into its own CLASS file.
This work is licensed under a Creative CommonsAttribution 3.0 Unported License (including images &stylesheets). The source is available onGithub.
Preliminaries
Note that for the examples below, 'io' is an alias forclojure.java.io. That is, it's assumed your
ns
macro contains:![Java Files Create File In Directory Java Files Create File In Directory](https://docs.rapidminer.com/latest/developers/creating-your-own-extension/img/folder-structure.png)
or else in the repl you've loaded it:
Recipes
Read a file into one long string
Note, you can pass urls to
slurp
as well. See also slurp atClojuredocs.Read a file one line at a time
Suppose you'd like to call
my-func
on every line in a file,and return the resulting sequence:The
doall
is needed because the map
call is lazy. The lines thatline-seq
gives you have no trailing newlines (and empty lines in thefile will yield empty strings (')).![Java Files Create File In Directory Java Files Create File In Directory](https://attacomsian.com/social-banner/java-create-temporary-directory.png)
Write a long string out to a new file
Overwrites the file if it already exists. To append, use
Write a file one line at a time
Suppose you'd like to write out every item in a vector, one item perline:
Check if a file exists
Is it a directory? :
An io/file is a java.io.File object (a file or a directory). You cancall a number of functions on it, including:
To read about more available methods, see the java.io.Filedocs.
Get a list of the files and dirs in a given directory
As
File
objects:Same, but just the names (strings), not File objects:
The results of those calls are seqable.
Java Add File To Directory
See also
- the I/O section of the cheatsheet
Contents
How to filter list of files or sub-directories in a folder using java ?
We can get the list of files and sub-directories in a given folder using Java. We can also filter the list based on extension or a specific condition.
In this Java Tutorial, we shall learn how to perform the following tasks.
How to extract list of files and directories in a folder using Java?
Follow these steps.
Java File Directory Example
Step 1 : Specify the folder. In this example, “sample” is the folder name placed at the root to the project.
File folder=newFile('sample'); |
Step 2 : Get the list of all items in the folder.
File[] listOfFiles=folder.listFiles(); |
Step 3 : Check if an item in the folder is a file.
listOfFiles[i].isFile() |
Step 4 : Check if an item in the folder is a directory.
listOfFiles[i].isDirectory() |
Complete program that lists the files and directories in a folder is given below.
import java.io.File; import java.util.List; /** * Program that gives list of files or directories in a folder using Java publicclassListOfFilesExample { publicstaticvoidmain(String[] args) { List directories=newArrayList<>(); File folder=newFile('sample'); if (listOfFiles[i].isFile()) { } elseif (listOfFiles[i].isDirectory()) { } System.out.println('List of files :n---------------'); for(StringfName:files) System.out.println(fName); System.out.println('nList of directories :n---------------------'); for(StringdName:directories) System.out.println(dName); } |
The sample folder and its contents are as shown in the below picture :
list of files and directories in “sample” folder
When the program is run, Output to the console would be as shown below. Blogtouch for blogger 2 1 0.
List of files: html_file_1.html text_file_3.txt --------------------- directory_2 |
How to get list of files that belong to a specific file-type (a specific extension) from the folder ?
Follow these steps.
Step 1 : Specify the folder.
File folder=newFile('sample'); |
Step 2 : Get the list of all items in the folder.
File[] listOfFiles=folder.listFiles(); |
Step 3 : Check if an item in the folder is a file.
listOfFiles[i].isFile() |
Step 4 : Check if the file belong to specified file-type.
Check the extension of the filename with String.endsWith(suffix)
listOfFiles[i].getName().endsWith(fileExtension) |
Java Files Create File In Directory Folder
Complete program to get the list of files, that belong to a specified extension type is shown below.
import java.io.File; import java.util.List; publicclassListOfFilesExample { publicstaticvoidmain(String[] args) { List directories=newArrayList<>(); File[] listOfFiles=folder.listFiles(); for (inti=0; i< listOfFiles.length; i++) { if(listOfFiles[i].getName().endsWith(fileExtension)){ } } System.out.println('List of .txt files :n--------------------'); for(StringfName:files) System.out.println(fName); } |
When the program is run, Output to the console would be as shown below.
List of .txt files: text_file_1.txt text_file_2.txt |
How to get list of files that belong a specific file type from the folder and its sub folders/directories ?
Follow these steps.
Step 1 : Specify the folder.
File folder=newFile('D:'+File.separator+'Arjun'+File.separator+'sample'); |
Step 2 : Get the list of all items in the folder.
File[] listOfFiles=folder.listFiles(); |
Step 3 : Check if an item in the folder is a file.
listOfFiles[i].isFile() |
Step 4 : Check if the file belong to specified file-type.
Check the extension of the filename with String.endsWith(suffix)
listOfFiles[i].getName().endsWith(fileExtension) |
Step 5 : Check if an item in the folder is a directory.
listOfFiles[i].isDirectory() |
Java Get Directory Files
Step 6 : Get the list of files (beloging to specific file-type) recursively from all the sub-directories.
The complete program is given below.
import java.io.File; import java.util.List; publicclassFileListExtractor { List allTextFiles=newArrayList(); FileListExtractor folderCrawler=newFileListExtractor(); System.out.println('All text files in the folder 'sample' and its sub directories are :n---------------------------------------------------------------'); for(StringtextFileName:folderCrawler.allTextFiles){ } File folder=newFile(path); if (listOfFiles[i].isFile()) { if(listOfFiles[i].getName().endsWith(fileExtension)){ } // if a directory is found, crawl the directory to get text files crawlFold(path+File.separator+listOfFiles[i].getName()); } } |
When the program is run, Output to the console is :
All text files inthe folder'sample'andits sub directories are: --------------------------------------------------------------- text_file_1_1.txt text_file_1_1.txt text_file_1_2.txt text_file_3.txt |
Conclusion
In this Java Tutorial, we learned how to list all the files and sub-directories in a given folder.