1

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?

locorecto
  • 1,178
  • 3
  • 13
  • 40
  • the default bufferSize is 8192 characters, do you need it larger? this value should be enough for I/O operations and you should not have performance issues – Claudiu Oct 02 '13 at 12:52
  • how are you going to write the data?! –  Oct 02 '13 at 12:52
  • @csoroiu I am combining large log files 100MB to 500MB. I wanted to use at least 500 KB buffers. – locorecto Oct 02 '13 at 12:57
  • @user2511414 can you be more specific, I didn't understand your question. – locorecto Oct 02 '13 at 12:58
  • how are you going to use the `BufferedWrite?` what kind of data are you going to write? byte? String? Object?! –  Oct 02 '13 at 12:59
  • @user2511414 I am going to write strings into a file line by line. – locorecto Oct 02 '13 at 13:00
  • 1
    if you are trying to combine files, why not using transferTo function? if you do not have to do some special logic, that fuction works faster. http://stackoverflow.com/questions/18622768/java-nio-transferto-seems-to-be-impossibly-fast – Claudiu Oct 02 '13 at 13:09
  • I would have my own `OutputStream`, where it contains a (n-length) byte array as buffer. it's not a hard work really. –  Oct 02 '13 at 13:14
  • 500K is ridiculously large for an I/O buffer. Keep cutting it in half until you notice an actual difference. I suspect that won't occur until you get below 64k. – user207421 Oct 03 '13 at 01:19

1 Answers1

2

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.

Community
  • 1
  • 1
Pyranja
  • 3,529
  • 22
  • 24