【问题标题】:File.createNewFile() failing in java (Ubuntu 12.04)File.createNewFile() 在 java (Ubuntu 12.04) 中失败
【发布时间】:2013-09-14 23:32:05
【问题描述】:

我正在尝试在 java 中创建 NewFile()。我已经写下了以下示例。我已经编译了它,但是遇到了运行时错误。

import java.io.File;
import java.io.IOException;


public class CreateFileExample
{
    public static void main(String [] args)
    {


            try
            {
                    File file = new File("home/karthik/newfile.txt");

                    if(file.createNewFile())
                    {
                            System.out.println("created new fle");
                    }else
                    {
                            System.out.println("could not create a new file");
                    }
            }catch(IOException e )
            {
                    e.printStackTrace();
            }

    }

}

它正在编译。我得到的运行时错误是

java.io.IOException: No such file or directory
    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:947)
    at CreateFileExample.main(CreateFileExample.java:16)

【问题讨论】:

标签: java file temporary-files


【解决方案1】:

这里有几点

1- 正如维克多所说,您缺少前导斜杠

2-如果你的文件被创建了,那么每次你调用这个方法“File.createNewFile()”都会返回false

3- 您的类非常依赖于平台(Java 是强大的编程语言的主要原因之一是它不依赖于平台),相反,您可以使用 System.getProperties() 检测相对位置抛出:

    // get System properties :
    java.util.Properties properties = System.getProperties();

    // to print all the keys in the properties map <for testing>
    properties.list(System.out);

    // get Operating System home directory
    String home = properties.get("user.home").toString();

    // get Operating System separator
    String separator = properties.get("file.separator").toString();

    // your directory name
    String directoryName = "karthik";

    // your file name
    String fileName = "newfile.txt";


    // create your directory Object (wont harm if it is already there ... 
    // just an additional object on the heap that will cost you some bytes
    File dir = new File(home+separator+directoryName);

    //  create a new directory, will do nothing if directory exists
    dir.mkdir();    

    // create your file Object
    File file = new File(dir,fileName);

    // the rest of your code
    try {
        if (file.createNewFile()) {
            System.out.println("created new fle");
        } else {
            System.out.println("could not create a new file");
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

这样您将在任何平台上的任何主目录中创建文件,这适用于我的 Windows 操作系统,预计也适用于您的 Linux 或 Ubuntu

【讨论】:

    【解决方案2】:

    您在文件路径中缺少前导斜杠。

    试试这个:

    File file = new File("/home/karthik/newfile.txt");
    

    应该可以!

    【讨论】:

    • @liv2hak:如果可行,请也“接受”作为答案!谢谢:-)
    【解决方案3】:

    实际上,当没有目录“karthik”时出现此错误,如上例所示,createNewFile() 只是创建文件而不是目录,使用 mkdir() 作为目录,然后使用 createNewFile() 作为文件。

    【讨论】:

      猜你喜欢
      • 2014-05-25
      • 2014-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多