Here you can find the source of saveAsFile(File targetFile, ByteBuffer bb)
public static void saveAsFile(File targetFile, ByteBuffer bb) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; public class Main { /**//from w w w .java2s.c o m * Save the string into the file. The created file is UTF-8 encoded. * * @param targetFile * @param s * @throws IOException */ public static void saveAsFile(File targetFile, String s) throws IOException { FileOutputStream fos = new FileOutputStream(targetFile); try { OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); osw.write(s); osw.close(); } finally { fos.close(); } } /** * Save the string into the file. The created file is {charset} encoded. * * @param targetFile * @param charset * @param s * @throws IOException * @since 3.0 */ public static void saveAsFile(File targetFile, Charset charset, String s) throws IOException { FileOutputStream fos = new FileOutputStream(targetFile); try { OutputStreamWriter osw = new OutputStreamWriter(fos, charset); osw.write(s); osw.close(); } finally { fos.close(); } } /** * Save the given content into the given file. * * @param targetFile * @param s * @throws IOException */ public static void saveAsFile(File targetFile, byte[] s) throws IOException { FileOutputStream fos = new FileOutputStream(targetFile); try { fos.write(s); } finally { fos.close(); } } public static void saveAsFile(File targetFile, ByteBuffer bb) throws IOException { FileOutputStream fos = new FileOutputStream(targetFile); try { FileChannel wChannel = fos.getChannel(); while (bb.hasRemaining()) { wChannel.write(bb); } wChannel.close(); } finally { fos.close(); } } }