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

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

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

Java File Java java.nio.file.Files

Files.newBufferedWriter() method is used to create a BufferedWriter object that can efficiently write text to a specified file. Compared with traditional FileWriter , it provides buffering, which can significantly improve write performance.

Method Definition

public

static

BufferedWriter

newBufferedWriter

(

Path path, OpenOption...

options

)

throws

IOException

public

static

BufferedWriter

newBufferedWriter

(

Path path, Charset charset, OpenOption...

options

)

throws

IOException

Parameter analysis

Path path

Charset charset (optional)

- `StandardCharsets.UTF_8` - `StandardCharsets.ISO_8859_1` - `StandardCharsets.US_ASCII`

OpenOption... options (optional)

- `StandardOpenOption.CREATE` - Create if the file does not exist - `StandardOpenOption.APPEND` - Append to the end of the file - `StandardOpenOption.TRUNCATE_EXISTING` - Truncate existing file content - `StandardOpenOption.WRITE` - Open for writing

Basic Usage Example

Example 1: Basic writing

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

(

"output.txt"

)

;

       

       

try

(

BufferedWriter

writer

=

Files.

newBufferedWriter

(

path

)

)

{

            writer.

write

(

"Hello, World!"

)

;

            writer.

newLine

(

)

;

 

// Write a newline

            writer.

write

(

"This is a new line."

)

;

       

}

catch

(

IOException

e

)

{

            e.

printStackTrace

(

)

;

       

}

   

}

}

Example 2: Specify encoding and 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

(

"output_append.txt"

)

;

       

       

try

(

BufferedWriter

writer

=

Files.

newBufferedWriter

(

                path,

                StandardCharsets.

UTF_8

,

                StandardOpenOption.

CREATE

,

                StandardOpenOption.

APPEND

)

)

{

            writer.

write

(

"Appended line 1"

)

;

            writer.

newLine

(

)

;

            writer.

write

(

"Appended line 2"

)

;

       

}

catch

(

IOException

e

)

{

            e.

printStackTrace

(

)

;

       

}

   

}

}


Advanced usage

Batch write large amounts of data

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.util.stream.IntStream

;

public

class

LargeFileWriter

{

   

public

static

void

main

(

String

[

]

args

)

{

        Path path

=

Paths.

get

(

"large_file.txt"

)

;

       

       

try

(

BufferedWriter

writer

=

Files.

newBufferedWriter

(

path

)

)

{

            IntStream.

range

(

0

,

100000

)

.

forEach

(

i

->

{

               

try

{

                    writer.

write

(

"Line "

i

"\n"

)

;

               

}

catch

(

IOException

e

)

{

                    e.

printStackTrace

(

)

;

               

}

           

}

)

;

       

}

catch

(

IOException

e

)

{

            e.

printStackTrace

(

)

;

       

}

   

}

}


Best Practices

  1. Using try-with-resources: Ensure that BufferedWriter Automatically closed after use
  2. Handle Exceptions: Always handle possible IOException
  3. Choose the buffer size reasonably: The default buffer size is usually sufficient, but can be adjusted via the BufferedWriter constructor
  4. Consider encoding: Explicitly specify character encoding to avoid cross-platform issues
  5. Batch writing: For large amounts of data, consider batch writing rather than single writes

Frequently Asked Questions

Q1: Compared with FileWriter , what are the advantages?

newBufferedWriter() provides buffering, reducing the number of actual I/O operations and significantly improving write performance.

Q2: How to ensure the file is closed correctly?

Using the try-with-resources syntax ensures resources are automatically closed, even when an exception occurs.

Q3: What is the difference between append mode and overwrite mode?


Summary

Files.newBufferedWriter() is a powerful and flexible file writing tool in Java NIO. It combines buffering technology and multiple open options to provide an efficient and safe solution for file writing operations. Used properly, it can significantly improve the performance and reliability of file writing.

Java File Java java.nio.file.Files