I am studying about the File
class in Java, and would like to know the difference between using mkdir
and CreateNewFile
, does mkdir
create the directory that is passed as a parameter in folder format only?
I am studying about the File
class in Java, and would like to know the difference between using mkdir
and CreateNewFile
, does mkdir
create the directory that is passed as a parameter in folder format only?
One read in the documentation would be enough for you to answer your question:
public boolean createNewFile() throws IOException
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not exist yet.
For example:
File f = new File("test.txt");
System.out.println(f.createNewFile()); //true, criando o arquivo
File g = new File("dir_inexistente/test2.txt");
System.out.println(f.createNewFile()); //false, pois o diretorio nao existe
public boolean mkdir()
Creates the directory named by this abstract pathname.
There is still the mkdirs()
method, which creates a parent directory if it does not exist. For example:
File f = new File("diretorio_pai_inexistente/diretorioX");
System.out.println(f.mkdir()); //false, o diretório pai não existe, logo, 'diretorioX' não é criado.
System.out.println(f.mkdirs()); //true e cria tanto o diretório pai quanto 'diretorioX'.