What is the difference between Mkdir and CreateNewFile?

-2

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?

    
asked by anonymous 12.06.2018 / 18:00

1 answer

6

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'.
    
12.06.2018 / 18:18