I ran into a problem with BufferedWriter when writing data to a single file with some streams.
I set the size of the BufferedWriter buffer, but no matter what number I set, it flushes the data to disk when the buffer is 8192 (the default buffer size), and not the size I set (here is 16384), is there a problem with my code?
This is how I create a BufferedWriter :
new BufferedWriter(new FileWriter(fileName, true), 16384);
This is the full code:
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class Test1 { public static void main(String[] args) throws IOException { for(int i =0;i<10;i++){ MyThread r = new MyThread(); Thread t = new Thread(r); t.start(); } } } class MyThread implements Runnable { public void run() { String s = "{addffffffkkkljlkj2015dd}\n"; BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter( "/Users/liaoliuqing/Downloads/1.txt", true),16384); } catch (IOException e) { e.printStackTrace(); } for(int i =0 ; i<1000; i++){ try { bw.write(String.format("%03d", i)+s);
source share