In fact, you often have to manipulate files and folders in system including adding, deleting, renaming. Java provides you 2 classes to do this:
The java.io.File class represents a file or a directory of system and it has included in Java since version 1.0.
In Version 7.0, Java adds the java.nio.file.Files class, which simplifies the manipulation of files and directories, all the methods of the class are static and nature.
In the post, I just mention the use of the java.io.File class, you can see more about java.nio.file.Files here:
The java.io.File class represents a file or a directory in the system, which represents a pathname. The example below creates an file representing a path on Windows or Unix:
java.io.File can represent a path that may not actually exist on the system. If it exists, it may be a folder or a file.
The simple example below creates a File object that represents a path, and checks for its existence, print out basic information if that path actually exists:
The File class provides 2 methods to create a directory:
|public boolean mkdir()
|Create directory given by path. Note directory is only created if the parent directory exists.
|public boolean mkdirs()
|Create the directory given by the path, including the parent directory if it does not exist.
The java.io.File provide some methods to retrieve subfiles and subdirectories in a directory. And use FileFilter to filer these files.
|static File listRoots()
|Returns an array of File objects that represent the root directories. In Windows it will be the drives (C :, :D, ..), in Unix it is /
|The method returns an array of pathnames for files and directories in the current directory.
|File listFiles(FilenameFilter filter)
|The method returns an array of pathnames for files and directories in the current directory, and match the FilenameFilter filter on the parameter.
|File listFiles(FileFilter filter)
|The method returns an array of pathnames for files and directories in the current directory, and match the FileFilter filter on the parameter.
|The method returns an array of paths for files and directories in the current directory.
|String list(FilenameFilter filter)
|The method returns an array of paths for files and directories in the current directory, and match the FilenameFilter filter on the parameter.
For example, list all of the root directory:
The example below lists all direct subfiles and subdirectories of a directory:
The simple example below uses FileFilter to filter out files in a directory with the "txt" extension:
The example of using FilenameFilter:
The java.io.File class provides you with some methods to list directories and files are "direct children" of the current directory. The example below use recursion to list all descendant directories and descendant files of a directory.
When using the rename() method of the File class, you can change name (Or the path) of a file or a directory. If you change the path, make sure that your new parent directory has already existed.
The example below renames a file (Or directory) but does not change the parent paths.
Renaming includes the parent path:
Renaming and changing the parent path are similar to the "Cut" action of a file or a directory to another directory and then renaming it.
Note: For Windows OS, the File.rename() method will not work if you change from the path to another drive.