It will throw a FileNotFoundException if the file doesn't exist and cannot be created (doc), but it will create it if it can. To be sure you probably should first test that the file exists before you create the FileOutputStream (and create with createNewFile() if it doesn't):

File yourFile = new File("score.txt");
yourFile.createNewFile(); // if file already exists will do nothing 
FileOutputStream oFile = new FileOutputStream(yourFile, false); 
Answer from talnicolas on Stack Overflow
🌐
Codemia
codemia.io › home › knowledge hub › java fileoutputstream create file if not exists
Java FileOutputStream Create File if not exists | Codemia
July 30, 2025 - Yes, FileOutputStream will create the target file if it does not already exist, as long as the parent directory exists and your process has permission to write there.
🌐
Blogger
watchmouse.blogspot.com › 2017 › 06 › java-fileoutputstream-create-file-or.html
Watch Mouse: Java FileOutputStream Create File or Parent Folders if not exists
up vote down vote FileUtils from apache commons is a pretty good way to achieve this in a single line. FileOutputStream s = FileUt...
🌐
Java2s
java2s.com › example › java › file-path-io › opens-a-fileoutputstream-for-the-specified-file-checking-and-creating.html
Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist. - Java File Path IO
*/ //package com.java2s; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] argv) throws Exception { File file = new File("Main.java"); System.out.println(openOutputStream(file)); } /** * Opens a {@link FileOutputStream} for the specified file, checking and * creating the parent directory if it does not exist.
🌐
ZetCode
zetcode.com › java › createfile
Java create file - learn how to create a file in Java
The file is created when FileOutputStream object is instantiated. If the file already exists, it is overridden. FileNotFoundException is thrown 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.
🌐
The Coding Forums
thecodingforums.com › archive › archive › java
FileOutputStream Does Not Create File? | Java | Coding Forums
April 25, 2008 - Can a FileOutputStream create a directory if it does not already exist? Click to expand... No. You need to use a File object to first create the directory, e.g. File.mkdirs() Thomas · L · Apr 25, 2008 ·
🌐
Coderanch
coderanch.com › t › 708245 › java › Creating-directory-doesn-exist-writing
Creating a directory if it doesn't exist and then writing a file (I/O and Streams forum at Coderanch)
April 2, 2019 - Both mkdir and mkdirs returns false if the directory already exists, so it's not necessary to check for existence first. I tend to use mkdirs because it will create all intermediate directories as well.
Find elsewhere
🌐
javaspring
javaspring.net › blog › java-fileoutputstream-create-file-if-not-exists
How to Use Java FileOutputStream to Create a File If It Doesn't Exist — javaspring.net
It is part of Java’s byte-stream ... data). ... Writes bytes to a file. Creates a file automatically if it does not exist (if the parent directory exists and permissions allow)....
🌐
W3Docs
w3docs.com › java
Java FileOutputStream Create File if not exists | W3Docs
import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void main(String[] args) { try (FileOutputStream fos = new FileOutputStream("output.txt")) { // File is created automatically if it does not exist } catch (IOException e) { e.printStackTrace(); } } }
🌐
Baeldung
baeldung.com › home › java › java io › java – create a file
Java - Create a File | Baeldung
August 29, 2024 - In this case, a new file is created when we instantiate the FileOutputStream object. If a file with a given name already exists, it will be overwritten. If, however, the existing file is a directory or a new file cannot be created for any reason, then we’ll get a FileNotFoundException. Additionally, note we used a try-with-resources statement – to be sure that a stream is properly closed.
🌐
Oracle
docs.oracle.com › cd › E17802_01 › j2se › j2se › 1.5.0 › jcp › beta1 › apidiffs › java › io › FileOutputStream.html
java.io Class FileOutputStream
January 30, 2022 - First, if there is a security manager, its checkWrite method is called with name 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.
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › io › FileOutputStream.html
FileOutputStream (Java SE 10 & JDK 10 )
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. ... Invoking this constructor with the parameter name is equivalent to invoking new FileOutputStream(name, false).
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 85302 › can-java-create-dos-directories
Can JAVA create DOS directories? [SOLVED] | DaniWeb
Try doing the same thing, creating the FileOutputStream object from a File object after checking if the directory/file exists. If it doesn't exist, use the "mkdir()" method from File to create the directory. ... Something new.
🌐
Jenkov
jenkov.com › tutorials › java-io › file.html
Java File
December 17, 2019 - Create a directory if it does not exist. Read the length of a file. Rename or move a file. Delete a file. Check if path is file or directory. Read list of files in a directory. This Java File tutorial will tell you more about how.
Top answer
1 of 2
1

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();
    }
}
  • In this case it is only necessary to call the topmost stream handler close() method - the BufferedOutputStream's one:

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 (e.g. "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, 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"));

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

2 of 2
-2

Then it means there is a file already in your directory

🌐
ExceptionsHub
exceptionshub.com › java-fileoutputstream-create-file-if-not-exists.html
Java FileOutputStream Create File if not exists | ExceptionsHub
November 18, 2017 - ... FileUtils from apache commons is a pretty good way to achieve this in a single line. FileOutputStream s = FileUtils.openOutputStream("/home/nikhil/somedir/file.txt") This will create parent folders if do not exist and create a file if not exists and throw a exception if file object is a ...
🌐
Solved
code.i-harness.com › en › q › 92cccb
not - java write to file - Solved
FileOutputStream s = FileUtils.openOutputStream("/home/nikhil/somedir/file.txt") This will create parent folders if do not exist and create a file if not exists and throw a exception if file object is a directory or cannot be written to.