Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java java.nio.file.Files newBufferedWriter() Method

Detailed explanation of the Java java.nio.file.Files newBufferedWriter() method

Java File Java java.nio.file.Files


java.nio.file.Files.newBufferedWriter() is a practical method provided in the Java NIO (New I/O) package for efficiently writing text files. It returns a BufferedWriter object that can be used to write text data to a file.


Method Definition

public

static

BufferedWriter

newBufferedWriter

(

Path path, OpenOption...

options

)

throws

IOException

public

static

BufferedWriter

newBufferedWriter

(

Path path, Charset cs, OpenOption...

options

)

throws

IOException

Parameter Description

path parameter

cs parameter (optional)

options parameter (optional)

- `StandardOpenOption.CREATE`: Create the file if it does not exist - `StandardOpenOption.APPEND`: Append to the end of the file - `StandardOpenOption.TRUNCATE_EXISTING`: Truncate the existing file - `StandardOpenOption.WRITE`: Open in write mode

Return Value

Returns a BufferedWriter object that can be used to efficiently write text data.

Method Characteristics

  1. Efficiency: Use buffering to reduce the number of actual I/O operations
  2. Convenience: Automatically handle file opening and closing (together with try-with-resources)
  3. Flexibility: Support multiple character encodings and open options

Usage examples

Example 1: Basic usage (UTF-8 encoding)

Example

import

java.io.BufferedWriter

;

import

java.io.IOException

;

import

java.nio.file.Files

;

import

java.nio.file.Path

;

import

java.nio.file.Paths

;

import

java.nio.file.StandardOpenOption

;

public

class

BufferedWriterExample

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path path

=

Paths.

get

(

"example.txt"

)

;

       

       

try

(

BufferedWriter

writer

=

Files.

newBufferedWriter

(

path

)

)

{

            writer.

write

(

"Hello, World!"

)

;

            writer.

newLine

(

)

;

 

// Write a newline

            writer.

write

(

"This is a test file."

)

;

       

}

catch

(

IOException

e

)

{

            e.

printStackTrace

(

)

;

       

}

   

}

}

Example 2: Specify encoding and open options

Example

import

java.io.BufferedWriter

;

import

java.io.IOException

;

import

java.nio.charset.StandardCharsets

;

import

java.nio.file.Files

;

import

java.nio.file.Path

;

import

java.nio.file.Paths

;

import

java.nio.file.StandardOpenOption

;

public

class

BufferedWriterExample2

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path path

=

Paths.

get

(

"example_append.txt"

)

;

       

       

try

(

BufferedWriter

writer

=

Files.

newBufferedWriter

(

                path,

                StandardCharsets.

UTF_8

,

                StandardOpenOption.

CREATE

,

                StandardOpenOption.

APPEND

)

)

{

            writer.

write

(

"This line will be appended."

)

;

            writer.

newLine

(

)

;

       

}

catch

(

IOException

e

)

{

            e.

printStackTrace

(

)

;

       

}

   

}

}


Best Practices

  1. Always use try-with-resources: Ensure that BufferedWriter closes correctly
  2. Handle Exceptions: Catch and handle appropriately IOException
  3. Consider encoding: Explicitly specify the character encoding to avoid platform dependency issues
  4. Use buffering reasonably: For writing large amounts of data, consider manually calling flush() Method

Frequently Asked Questions

Q: What is the difference between this method and the traditional FileWriter?

A: newBufferedWriter() provides a buffering mechanism and is usually more efficient than using FileWriter directly, especially when frequently writing small amounts of data.

Q: What happens if the file already exists?

A: By default, it overwrites the existing file. If you need to append content, use the StandardOpenOption.APPEND option.

Q: Why is the Chinese content in my file not displayed correctly?

A: It may be due to an encoding issue; make sure the correct character set is specified, such as StandardCharsets.UTF_8.


Summary

Files.newBufferedWriter() The method is an efficient tool for text file writing in Java NIO. It combines a buffering mechanism with flexible configuration options and is the recommended way to handle file writing tasks. By using its parameters and options reasonably, it can meet various file writing needs.

Java File Java java.nio.file.Files