简体   繁体   中英

FileOutputStream does not create file

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?

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 resources associated with the stream. The close method of FilterOutputStream calls its flush method, and then calls the close method of its underlying output stream.

  • 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 (eg "closes the file") as stated in the apidoc quote above. Using the finally block is a good practice:

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.

  • The FileOutputStream constructor creates an empty file on the disk:

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 .

Where a FileDescriptor is:

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.

This code should either produce a file or throw an exception. You have even confirmed that no conditions for throwing exception are met, eg 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"));

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

这意味着您的目录中已经有一个文件

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM