java FileOutputStream 不创建文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14378898/
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 16:04:40  来源:igfitidea点击:

FileOutputStream does not create file

javafilefileoutputstream

提问by Mercenary

I actually checked other posts that could be related to this and I couldn't find any answer to my question. So, had to create this newly:

我实际上检查了可能与此相关的其他帖子,但我找不到我的问题的任何答案。所以,不得不新创建这个:

The file does not get created in the given location with this code:

该文件不会使用以下代码在给定位置创建:

    File as = new File ("C:\Documents and Settings\<user>\Desktop\demo1\One.xls");
    if (!as.exists()) {
        as.createNewFile();
    }
    FileOutputStream fod = new FileOutputStream(as);
    BufferedOutputStream dob = new BufferedOutputStream(fod);
    byte[] asd  = {65, 22, 123};
    byte a1 = 87;
    dob.write(asd);
    dob.write(a1);
    dob.flush();

    if (dob!=null){
        dob.close();
    }
    if(fod!=null){
        fod.close();

The code runs fine and I don't get any FileNotFoundException!! Is there anything that I'm missing out here?

代码运行良好,我没有收到任何 FileNotFoundException !有什么我在这里遗漏的吗?

回答by linski

You can rewrite your code like this:

你可以像这样重写你的代码:

BufferedOutputStream dob = null;
try {
    File file = new File("C:\Documents and Settings\<user>\Desktop\demo1\One.xls");
    System.out.println("file created:" + file.exists());
    FileOutputStream fod = new FileOutputStream(file);
    System.out.println("file created:" + file.exists());
    BufferedOutputStream dob = new BufferedOutputStream(fod);
    byte[] asd = {65, 22, 123};
    byte a1 = 87;
    dob.write(asd);
    dob.write(a1);
    //dob.flush();
} 
catch (Exception ex) {
    ex.printStackTrace();
}
finally {
    if (dob != null) {
        dob.close();
    }
}

Closes this output stream and releases any system resourcesassociated with the stream. The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.

关闭此输出流并释放与该流关联的所有系统资源FilterOutputStream的close方法调用其flush方法,然后调用其底层输出流的close方法。

  • so, the dob.flush()in tryblock is commented out because the dob.close()line in the finallyblock flushes the stream. Also, it releases the system resources (e.g. "closes the file") as stated in the apidoc quote above. Using the finally blockis a good practice:
  • 因此,dob.flush()intry块被注释掉,因为块中的dob.close()finally刷新了流。此外,它会释放系统资源(例如“关闭文件”),如上面的 apidoc 引用所述。使用finally 块是一个很好的做法:

The finally block always executeswhen the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

finally块始终执行的时候try块退出。这确保即使发生意外异常也能执行 finally 块。但 finally 不仅仅用于异常处理——它允许程序员避免通过 return、continue 或 break 意外绕过清理代码。将清理代码放在 finally 块中始终是一个好习惯,即使没有预料到异常也是如此。

  • The FileOutputStream constructorcreates an empty file on the disk:
  • FileOutputStream构造函数在磁盘上创建一个空文件:

Creates a file output stream to write to the file represented by the specified File object. A new FileDescriptor object is created to represent this file connection.First, if there is a security manager, its checkWrite method is called with the path represented by the file argument as its argument.

If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.

创建文件输出流以写入由指定的 File 对象表示的文件。创建一个新的 FileDescriptor 对象来表示此文件连接。首先,如果有一个安全管理器,它的 checkWrite 方法被调用,以 file 参数表示的路径作为它的参数。

如果文件存在但是是一个目录而不是常规文件,不存在但无法创建,或者由于任何其他原因无法打开,则抛出 FileNotFoundException

Where a FileDescriptoris:

FileDescriptor在哪里:

Instances of the file descriptor class serve as an opaque handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes. The main practical use for a file descriptor is to create a FileInputStream or FileOutputStream to contain it.

Applications should not create their own file descriptors.

文件描述符类的实例充当底层机器特定结构的不透明句柄,表示打开的文件、打开的套接字或另一个字节源或接收器。文件描述符的主要实际用途是创建一个 FileInputStream 或 FileOutputStream 来包含它。

应用程序不应创建自己的文件描述符。

This code should either produce a file or throw an exception. You have even confirmed that no conditions for throwing exception are met, e.g. you are replacing the string and the demo1 directory exists. Please, rewrite this to a new empty file and run.

此代码应生成文件或引发异常。您甚至已经确认不满足抛出异常的条件,例如您正在替换字符串并且存在 demo1 目录。请将此重写为一个新的空文件并运行。

If it still behaving the same, unless I have missed something this might be a bug. In that case, add this line to the code and post output:

如果它仍然表现相同,除非我错过了一些东西,这可能是一个错误。在这种情况下,将此行添加到代码并发布输出:

 System.out.println(System.getProperty("java.vendor")+" "+System.getProperty("java.version"));

Judging from the path, I'd say you are using Win 7, am I right? What version?

从路径来看,我会说您使用的是Win 7,对吗?什么版本?

回答by Govind Balaji

Then it means there is a file already in your directory

那么这意味着你的目录中已经有一个文件