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

后端 未结 2 2137
花落未央
花落未央 2021-02-13 03:39

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

File file = new File(dirName + \"/\" + fileName);
try
{
    // -->          


        
2条回答
  •  既然无缘
    2021-02-13 04:34

    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;
    }
    

提交回复
热议问题