Java java.nio.file.Files.newOutputStream() Method
Files.newOutputStream() Java java.nio.file.Files newOutputStream() Method
Method Definition
public
static
OutputStream
newOutputStream
(
Path path, OpenOption...
options
)
throws
IOException
Parameter Description
Path path
- The method is used to open a file and return an output stream for writing byte data to the file. If the file does not exist, a new file can be created; if the file already exists, you can specify whether to overwrite or append content.
- Use
Paths.get()The file path to open or create - Example:
Path path = Paths.get("example.txt");
OpenOption... options (optional)
- method to create a Path object
- Options specifying how the file is opened
StandardOpenOptionCommon options come from the CREATE- Create a new file if it does not existCREATE_NEWenum:WRITE- Create a new file, throwing an exception if the file already existsAPPEND- Open the file for writingTRUNCATE_EXISTING- Open the file in append mode (write at the end of the file)
Return Value
Returns a OutputStream - `CREATE` - Create a new file if it does not exist - `CREATE_NEW` - Create a new file, throwing an exception if it already exists - `WRITE` - Open the file for writing - `APPEND` - Open the file in append mode (write at the end of the file) - `TRUNCATE_EXISTING` - If the file already exists, clear its content
Usage examples
object that can be used to write byte data to the file.
Example
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardOpenOption
;
public
class
FileWriteExample
{
public
static
void
main
(
String
[
]
args
)
{
Path path
=
Paths.
get
(
"example.txt"
)
;
try
(
OutputStream
outputStream
=
Files.
newOutputStream
(
path,
StandardOpenOption.
CREATE
,
StandardOpenOption.
WRITE
)
)
{
String
content
=
"Hello, Java NIO!"
;
outputStream.
write
(
content.
getBytes
(
)
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
}
Example 1: Basic usage - Create a new file and write content
Example
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardOpenOption
;
public
class
FileAppendExample
{
public
static
void
main
(
String
[
]
args
)
{
Path path
=
Paths.
get
(
"example.txt"
)
;
try
(
OutputStream
outputStream
=
Files.
newOutputStream
(
path,
StandardOpenOption.
CREATE
,
StandardOpenOption.
APPEND
)
)
{
String
additionalContent
=
"\nAdding more content!"
;
outputStream.
write
(
additionalContent.
getBytes
(
)
)
;
}
catch
(
IOException
e
)
{
e.
printStackTrace
(
)
;
}
}
}
Exception handling
Files.newOutputStream() The method may throw the following exceptions:
-
IOException
-
UnsupportedOperationException
-
SecurityException
Best Practices
-
Using try-with-resources
-
- As shown in the example, use the try-with-resources statement to ensure the stream is closed properly - This avoids resource leaks
-
- Even if no options are specified, the method has default behavior - But specifying explicitly makes the code intent clearer
-
- Verify that the path is within the allowed directory range - Prevent path traversal attacks
The difference between FileOutputStream - If you need exclusive access to the file, consider using a file locking mechanism
-
Files.newOutputStream()Advantages
-
FileOutputStreamApplicable scenarios
Summary
Files.newOutputStream() - When compatibility with legacy code is needed - When only the most basic file writing functionality is needed FileOutputStreamis a convenient method for file writing in Java NIO, providing flexible options to control how files are opened. Compared with the traditional
, it integrates better with other NIO components and supports more advanced features. When writing new Java applications, it is recommended to use this method for file writing operations.
Java java.nio.file.Files