How to create a file in java (not a folder) ?

Gerard :

Perhaps somewhat embarassing, but after some hours I still cannot create a file in Java...

File file = new File(dirName + "/" + fileName);
try
{
    // --> ** this statement gives an exception 'the system cannot find the path'
    file.createNewFile();
    // --> ** this creates a folder also named a directory with the name fileName
    file.mkdirs();
    System.out.println("file != null");
    return file;
}
catch (Exception e)
{
    System.out.println(e.getMessage());
    return null;
}

What am I missing here?

bruno conde :

Try creating the parent dirs first:

File file = new File(dirName + File.separator + fileName);
try {
    file.getParentFile().mkdirs();
    file.createNewFile();
    System.out.println("file != null");
    return file;
}
catch (Exception e)
{
    System.out.println(e.getMessage());
    return null;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a file in a folder in java

From Java

How to create a folder in Java?

From Java

How to create a Map with folder and corresponding file using java8

From Dev

How to create a .txt file within the java project folder and write to it

From Dev

How to create jar file without java classes but with a folder using maven

From Java

how to create a Folder inside a folder in java

From Java

How to create empty folder in java?

From Java

Create a file and store in Java web application folder

From Dev

how to create nuspec file in specific folder?

From Dev

How to dynamically create Folder in batch file - CMD

From Dev

How to create a public folder/file in Android 10

From Dev

How to create file inside views folder in codeigniter

From Dev

How to create a symbolic/soft link to a file not a folder

From Java

How to create log file in in tomcat/logs folder

From Java

How can I create a file in each folder?

From Dev

How to create the folder structure and the file for a given path

From Dev

How to create folder shortcuts in file manager?

From Dev

How to create a file in a specific team drive or folder?

From Dev

How to create a folder from a string in a file?

From Dev

How to create a folder and a txt file in Delphi

From Dev

how to create a folder and put a txt file into it in VBA

From Dev

How to create folder per each line of a file?

From Dev

How to create a file in public folder according to environnement

From Dev

How to create a csv file in a specific folder in Python?

From Dev

How to create a .o file in a specific folder - Makefile?

From Dev

How to create a file in a project in a Solution Folder (not physical folder)

From Dev

How can I create a folder with the same name as the file names in that folder?

From Dev

create folder by that value and how to save file to that folder LARAVEL

From Java

Java NIO | Paths & Files | How to create custom file and folder with custom name fetched from other object?

Related Related

  1. 1

    How to create a file in a folder in java

  2. 2

    How to create a folder in Java?

  3. 3

    How to create a Map with folder and corresponding file using java8

  4. 4

    How to create a .txt file within the java project folder and write to it

  5. 5

    How to create jar file without java classes but with a folder using maven

  6. 6

    how to create a Folder inside a folder in java

  7. 7

    How to create empty folder in java?

  8. 8

    Create a file and store in Java web application folder

  9. 9

    how to create nuspec file in specific folder?

  10. 10

    How to dynamically create Folder in batch file - CMD

  11. 11

    How to create a public folder/file in Android 10

  12. 12

    How to create file inside views folder in codeigniter

  13. 13

    How to create a symbolic/soft link to a file not a folder

  14. 14

    How to create log file in in tomcat/logs folder

  15. 15

    How can I create a file in each folder?

  16. 16

    How to create the folder structure and the file for a given path

  17. 17

    How to create folder shortcuts in file manager?

  18. 18

    How to create a file in a specific team drive or folder?

  19. 19

    How to create a folder from a string in a file?

  20. 20

    How to create a folder and a txt file in Delphi

  21. 21

    how to create a folder and put a txt file into it in VBA

  22. 22

    How to create folder per each line of a file?

  23. 23

    How to create a file in public folder according to environnement

  24. 24

    How to create a csv file in a specific folder in Python?

  25. 25

    How to create a .o file in a specific folder - Makefile?

  26. 26

    How to create a file in a project in a Solution Folder (not physical folder)

  27. 27

    How can I create a folder with the same name as the file names in that folder?

  28. 28

    create folder by that value and how to save file to that folder LARAVEL

  29. 29

    Java NIO | Paths & Files | How to create custom file and folder with custom name fetched from other object?

HotTag

Archive