Java UTF File Write writeFile(byte[] data, String outfile)

Here you can find the source of writeFile(byte[] data, String outfile)

Description

write File

License

Open Source License

Declaration

public static void writeFile(byte[] data, String outfile) throws IOException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.Channels;
import java.nio.channels.FileChannel;

public class Main {
    public static void writeFile(byte[] data, String outfile) throws IOException {
        FileOutputStream fout = null;
        FileChannel fc = null;/* ww  w  . j  a  va2  s.c om*/
        try {
            fout = new FileOutputStream(outfile);
            fc = fout.getChannel();
            fc.transferFrom(Channels.newChannel(new ByteArrayInputStream(data)), 0, data.length);
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            if (fc != null)
                fc.close();
            if (fout != null)
                fout.close();
        }

    }

    public static void writeFile(String body, String outfile) throws Exception {
        byte[] bits = null;
        try {
            bits = body.getBytes("utf-8");
        } catch (Exception e) {
            bits = body.getBytes();
        }

        writeFile(bits, outfile);
    }
}

Related

  1. getUTF8FileWriter(File f)
  2. getUTF8Writer(OutputStream os)
  3. getUTF8Writer(String file)
  4. utf8Writer(File file)
  5. utf8Writer(final File f)
  6. writeFile(String fileName, ArrayList data, String outputFolder)
  7. writeFileUTF(String nom)
  8. writeStringAsUTFByteArrayToDataOutput(final DataOutput out, final String str)
  9. writeToFile(File outputFile, String content)