To create a file or directory in the filesystem, one needs first to construct a new File object using one of the constructors listed in the Files management section. For example, assuming that the file name is FileName.txt, the File object can be created as new File("FileName.txt"). If the file has to be created inside a directory, then either a path has to be added in front of the file name (when it is passed into the constructor) or one of the other three constructors has to be used. For example:
String path = "demo1" + File.separator + "demo2" + File.separator;
String fileName = "FileName.txt";
File f = new File(path + fileName);
Note the usage of File.separator instead of the slash symbol (/) or (\). That is because the File.separator returns the platform-specific slash symbol. And here is an example of another File constructor usage:
String path = "demo1" + File.separator + "demo2" + File.separator;
String fileName = "FileName.txt";
File f = new File(path, fileName);
Yet another constructor can be used as follows:
String path = "demo1" + File.separator + "demo2" + File.separator;
String fileName = "FileName.txt";
File f = new File(new File(path), fileName);
However, if you prefer or have to use a Universal Resource Identifier (URI), you can construct a File object like this:
String path = "demo1" + File.separator + "demo2" + File.separator;
String fileName = "FileName.txt";
URI uri = new File(path + fileName).toURI();
File f = new File(uri);
Then one of the following methods has to be invoked on the newly created File object:
- boolean createNewFile(): If a file with this name does not yet exist, creates a new file and returns true; otherwise, returns false
- static File createTempFile(String prefix, String suffix): Creates a file in the temporary-file directory
- static File createTempFile(String prefix, String suffix, File directory): Creates the directory; the provided prefix and suffix are used to generate the directory name
If the file you would like to create has to be placed inside a directory that does not exist yet, one of the following methods has to be used first, invoked on the File object that represents the filesystem path to the file:
- boolean mkdir(): creates the directory with the provided name
- boolean mkdirs(): Creates the directory with the provided name, including any necessary but nonexistent parent directories
And, before we look at a code example, we need to explain how the delete() method works:
- boolean delete(): Deletes the file or empty directory, which means you can delete the file but not all the directories as follows:
String path = "demo1" + File.separator + "demo2" + File.separator;
String fileName = "FileName.txt";
File f = new File(path + fileName);
f.delete();
Let's look at how to overcome this limitation in the following example:
String path = "demo1" + File.separator + "demo2" + File.separator;
String fileName = "FileName.txt";
File f = new File(path + fileName);
try {
new File(path).mkdirs();
f.createNewFile();
f.delete();
path = StringUtils.substringBeforeLast(path, File.separator);
while (new File(path).delete()) {
path = StringUtils.substringBeforeLast(path, File.separator);
}
} catch (Exception e) {
e.printStackTrace();
}
This example creates and deletes a file and all related directories. Notice our usage of org.apache.commons.lang3.StringUtils class, which we have discussed in the String utilities section. It allowed us to remove from the path the just deleted directory and to continue doing it until all the nested directories are deleted and the top level directory is deleted last.