hi
When I started exploring the NIO apis , i found that using FileChannel to write to a file using ByteBuffer is slower compared to normal IO write using BufferedWriter . I even mixed both IO and NIO to write to a file still its slower than using IO BufferedWriter using FileWriter .
//using NIO and IO
String str = "javaranch";
FileChannel fchan = new FileOutputStream("perf-mix.txt").getChannel();
BufferedWriter bf = new BufferedWriter(Channels.newWriter(fchan,"UTF-8"));
bf.write(str);
bf.close();
fchan.close();
The Time taken is slower than using BufferedWriter and FileWriter .
FileWriter fw = new FileWriter("perf-tio.txt", false);
BufferedWriter buf_writer = new BufferedWriter (fw);
buf_writer.write(str);
buf_writer.close();
fw.close();
When i use FileChanne write directly its way too slower than the above two,
FileChannel fc = new FileOutputStream("perf-nio.txt").getChannel();
ByteBuffer bbuf = ByteBuffer.allocate(1024);
bbuf.put(str.getBytes());
bbuf.flip();
fc.write(bbuf);
fc.close();
Am i doing something wrong or The New IO is slower when it comes to writing a String to a file .
Thanks in Advance