文件不是用 Java 创建的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11944415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:01:39  来源:igfitidea点击:

File Not Created in Java

java

提问by halphhurts

I'm sure I'm missing something basic here. I'm trying to create a new file on my drive, but I'm getting an error:

我确定我在这里遗漏了一些基本的东西。我正在尝试在我的驱动器上创建一个新文件,但出现错误:

Exception in thread "main" java.io.FileNotFoundException: C:\ProgramData\msena\test.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileReader.<init>(FileReader.java:55)
at net.meosoft.relatetoit.core.HibernateSessionFactory.main(HibernateSessionFactory.java:89)

My code at the moment is:

我目前的代码是:

    final File file = new File("C:\ProgramData\uname2\test.txt");
    final BufferedReader in = new BufferedReader(new FileReader(file));
    while(in.ready()) {
        System.out.println(in.readLine());
    }
    in.close();

What's wrong at the moment? I want to just read, even if it's there (so file should be made).

目前有什么问题?我只想阅读,即使它在那里(所以应该制作文件)。

回答by Teh Hippo

Java doesn't automatically check that File() exists, nor will it automatically create it if you ask it.

Java 不会自动检查 File() 是否存在,也不会在您询问时自动创建它。

You'll need to do one of the following:

您需要执行以下操作之一:

  1. Add in a check for the file's existence: if(file.exists()) { ... }.
  2. Add in a check, similar to above, but then if it doesn't exist, call: file.createNewFile();. This will make a new file on the file system for you to use.
  1. 添加检查文件是否存在:if(file.exists()) { ... }.
  2. 添加一个检查,类似于上面的,但如果它不存在,调用:file.createNewFile();。这将在文件系统上创建一个新文件供您使用。

If that still doesn't work, I'd check you have write permissions to that directory. :)

如果这仍然不起作用,我会检查您是否对该目录具有写权限。:)

回答by Jeffrey

The Fileclass represents the path to a file, not the file itself. If the file does not exist (!File.exists()), an exception will be thrown when you try to access it. Make sure the path to the file is correct and that you have permission to read from that location.

File类表示文件路径,而不是文件本身。如果文件不存在 ( !File.exists()),当您尝试访问它时将抛出异常。确保文件路径正确并且您有权从该位置读取。

If you want to create the file, you can use File.createNewFile().

如果要创建文件,可以使用File.createNewFile().

回答by Osama Ahmad

this is the method to create file.

这是创建文件的方法。

Formatter output;//pointer to an object that will write to a file
public void createFile(){
try{
  output = new Formatter("C:\ProgramData\uname2\test.txt");
  //test.txt is the name of the file to be created
  //create file in the same folder called test.txt
  //if existed overwrite it
    }
  catch(FileNotFoundException e){
  System.err.println("Error creating file"+e.getMessage());
 }

call createFile() in the main

在 main 中调用 createFile()

 CreateTextFile file = new CreateTextFile();
 file.createFile();

回答by Fazal

Check your file name. It should not contain any colon.

检查您的文件名。它不应包含任何冒号。