locorecto
locorecto

Reputation: 1203

BufferedWriter with Path parameter and buffer size parameter

I know I can do this (with the corresponding try and catch of course)

Path path = Paths.get(outputFieLocation);
BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8);

and this as well

BufferedWriter writer = new BufferedWriter(new FileWriter(outputFieLocation), 5 * 1024);

Is there any way to set a buffer size using Path as a parameter?

Upvotes: 1

Views: 5677

Answers (1)

Pyranja
Pyranja

Reputation: 3589

No, but you can use path.toFile() to turn a Path into an equivalent File object suitable for the constructor of FileWriter. Note that you should not use the FileWriter as it unfortunately does not allow to specify the Charset.

final File file = path.toFile();
BufferedWriter out = new BufferedWriter(
   new OutputStreamWriter(new FileOutputStream(file),"UTF-8"), bufferSize);

(from here)

If there is no specific reason to set a custom buffer size, use the Files.new... alternative, the JDK defaults are sensible.

Upvotes: 2

Related Questions