What is the difference between Mkdir and Createnewfile?

Asked

Viewed 259 times

-2

I’m studying about the class File in Java, and would like to know the difference between using the mkdir and the CreateNewFile, the mkdir creates the directory that is passed as parameter in folder format only?

  • 4

    mkdir creates a directory; CreateNewFile creates a file. That’s not the difference?

  • 1

    Does the answer answer answer your questions? Don’t forget to mark it as accepted by clicking on This attitude will help other people who come here with the same question

1 answer

7


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 yet exist.

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’s still the method mkdirs(), 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`.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.