Java ByteBuffer Save saveAsFile(File targetFile, ByteBuffer bb)

Here you can find the source of saveAsFile(File targetFile, ByteBuffer bb)

Description

save As File

License

Open Source License

Declaration

public static void saveAsFile(File targetFile, ByteBuffer bb)
            throws IOException 

Method Source Code

//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();
        }
    }
}

Related

  1. save(Path path, ByteBuffer bb)
  2. saveAsBMP(String filename, ByteBuffer pixel_data, int width, int height)
  3. saveAsTempFile(ByteBuffer byteBuffer, String extension)
  4. saveAsTGA(String filename, ByteBuffer pixel_data, int width, int height)
  5. saveBytesBufferToFile(ByteBuffer buf, String filename)
  6. savePageBitMask(ByteBuffer buffer, BitSet freePagesBitSet)