FileOutputStream不创建文件 [英] FileOutputStream does not create file

查看:876
本文介绍了FileOutputStream不创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上检查了可能与此相关的其他帖子,但找不到我的问题的任何答案.因此,必须重新创建一个:

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();

代码运行正常,我没有收到任何FileNotFoundException !! 有什么我想念的吗?

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

推荐答案

您可以像这样重写代码:

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

  • 在这种情况下,仅需要调用最顶层的流处理程序 BufferedOutputStream 的一个:
    • In this case it is only necessary to call the topmost stream handler close() method - the BufferedOutputStream's one:
    • 关闭此输出流,并释放与该流关联的所有系统资源. FilterOutputStream的close方法调用其flush方法,然后调用其基础输出流的close方法.

      • 因此,try块中的dob.flush()被注释掉了,因为finally块中的dob.close()行刷新了流.而且,它会释放上面apidoc引文中所述的系统资源(例如关闭文件").使用最终阻止是一个好习惯:
        • so, the dob.flush() in try block is commented out because the dob.close() line in the finally block flushes the stream. Also, it releases the system resources (e.g. "closes the file") as stated in the apidoc quote above. Using the finally block is a good practice:
        • try块退出时,最终块始终执行.这样可以确保即使发生意外异常,也可以执行finally块.但是,最后,它不仅对异常处理有用,它还使程序员可以避免清除代码被返回,继续或中断意外地绕过.将清理代码放在finally块中始终是一个好习惯,即使在没有预期例外的情况下也是如此.

          The finally block always executes when 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.

          • FileOutputStream 构造函数在磁盘上创建一个空文件:
            • The FileOutputStream constructor creates an empty file on the disk:
            • 创建文件输出流以写入由指定File对象表示的文件. 已创建一个新的FileDescriptor对象来表示此文件连接. 首先,如果有安全管理器,则以文件参数表示的路径作为参数来调用其checkWrite方法.

              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.

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

              FileDescriptor 的位置是:

              文件描述符类的实例充当基础机器特定结构的不透明句柄,这些特定于机器的结构表示打开的文件,打开的套接字或其他字节源或宿.文件描述符的主要实际用途是创建一个FileInputStream或FileOutputStream来包含它.

              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.

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

              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.

              如果它仍然具有相同的功能,除非我错过了某些东西,否则这可能是一个错误.在这种情况下,请将以下行添加到代码中并发布输出:

              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"));
              

              从路径上判断,我想说的是您使用的是Win 7,对吗?什么版本?

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

              这篇关于FileOutputStream不创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆