Java : How to work with file and directory
The
By using the static
java.nio.file
package (from Java 7) has improved the way we work with the file system.Create a directory
For creating a new directory, we get a
Path
object for the provided path. Then, we check if the directory exists. If not, we call the static createDirectories()
method of the Files
class.Create a File
For creating a file, we follow basically the same process. But we get a
Path
object that refers to the directory and file. If the file doesn't exist, we call createFile()
method of the Files
class.Iterate through Files in a directory
By using the static
newDirectoryStream()
method of the Files
class, you can list the files in a directory this way:
Comments
Post a Comment