- Details
- Written by Nam Ha Minh
- Last Updated on 30 July 2019 | Print Email
Get list of all files and folders using ‘listFiles' method; Sort the list using ‘Arrays.sort' method; Now, using a ‘for-loop', iterate through this list and get the name of each file or folder using ‘getName' If it is a file, we are printing ‘File ' before the file name and if it is a folder we are printing. Java Create and Write To Files. To create a file in a specific directory (requires permission), specify the path of the file and use double backslashes to. 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. The listFiles method is a part of File class.The function returns an array of Files denoting the files in a given abstract pathname if the path name is a directory else returns null. The function is an overloaded function.
renameTo() method of a Fileobject in the old File I/O API, or the Files.move() method in the new Java NIO API.1. Rename File/Directory Example with renameTo() method
You can use the renameTo() method to rename a file or directory to a new name which does not exist.The following example renames a file to a new name in the current directory:As you can see in this example, the renameTo() method returns a boolean value indicating the renaming succeeded (true) or failed (false) - so you should always check its return value.If the destination file exists, the method returns false.The following example renames the directory 'test' to 'dist' in the current directory:If the destination directory exists, the method return false.2. Move File Example with renameTo() method
If the path of the destination File points to another directory, the source file will be moved. The following example moves a file from the current directory to another one (also renames it):NOTE: You cannot use the renameTo() method to move directory, even the directory is empty.This method is platform-dependent and is not guaranteed to work in all cases. So it is recommended to use the Files.move() method in Java NIO as described below.3. Rename File/Directory Example with Files.move() method
The static move() method of the Files class in the java.nio.file package is platform-independent and have options to replace the destination file if exists: Files.move(Path source, Path target, CopyOptions… options)This method returns the path to the target file, and throws exception if the operation failed.The following example renames a file in the current directory:If the target file exists, this method throws java.nio.file.FileAlreadyExistsException. You can specify the option to replace the existing file like this:The following example renames a directory to a new one:If the target dir exists, it throws FileAlreadyExistsException. You can fore to replace the existing directory with the copy option:However, this works only if the target directory is not empty. Otherwise, it throws java.nio.file.DirectoryNotEmptyException.4. Move File/Directory Example with Files.move() method
The following example illustrates how to move a file from the current directory to another (keeping the same file name) and replace the existing file:And you can move an empty directory to another location, for example:NOTE: You can move only empty directory and replace existing directory if it is also empty. In either a directory is not empty, a DirectoryNotEmptyException is thrown.API References:
Related File IO Tutorials:
Other Java File IO Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.- Related Questions & Answers
- Selected Reading
The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.
To get the list of all the existing files in a directory this class provides the files class provides list() (returns names) and ListFiles (returns File objects) with different variants.
The List() method
This method returns a String array which contains the names of all the files and directories in the path represented by the current (File) object.
Using this method, you can just print the names of the files and directories.
Example
The following Java program lists the names of all the files and directories in the path D:ExampleDirectory.
Output
The ListFiles() method
This method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.
Since this method returns the objects of each file/directory in a folder. Using it you can access the properties of the files/directories such as size, path etc.
Example
Macgo blu ray player pro 3 1 download free. The following Java program prints the name, path and, size of all the files in the path D:ExampleDirectory.
Output
Example
The following Java program lists the names of all the files and directories in the path D:ExampleDirectory.
Output
The ListFiles() method
This method returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object.
Since this method returns the objects of each file/directory in a folder. Using it you can access the properties of the files/directories such as size, path etc.
Example
Macgo blu ray player pro 3 1 download free. The following Java program prints the name, path and, size of all the files in the path D:ExampleDirectory.
Output
The List(FilenameFilter filter) method
As suggested in its signature, this method accepts a FilenameFilter object and returns a String array containing the names of all the files and directories in the path represented by the current (File) object. But the retuned array contains the filenames which are filtered based on the specified filter.
Using this method, you can get the filtered names of the files and directories in a particular folder.
Example
The following Java program prints the names of the text files in the path D:ExampleDirectory.
Output
List of the text files in the specified directory −
The ListFiles(FilenameFilter filter) method
This method accepts a FilenameFilter object and returns a File array containing the objects of all the files and directories in the path represented by the current File object. But the retuned array contains the files (objects) which are filtered based on their name
Using this method, you can get the filtered file objects of the files and directories in a particular folder, according to their names.
Example
Java Files Create File In Directory Folder
The following Java program prints the name, path and, size of all the text files in the path D:ExampleDirectory.
Output
Java File Directory Example
The ListFiles(FileFilter filter) method
This method accepts a FileFilter object and returns a File array containing the (File) objects of all the files and directories in the path represented by the current File object. But the retuned array contains the files (objects) which are filtered based on the property of the file.
Using this method, you can get the filtered file objects of the files and directories in a particular folder based on the size, path, type (file or, directory) etc…
Example
The following Java program prints the name, path and, size of all the files (not folders) in the path D:ExampleDirectory.