Java File Write fileWrite(String path, int format, String content, Object bytesObj)

Here you can find the source of fileWrite(String path, int format, String content, Object bytesObj)

Description

file Write

License

Open Source License

Declaration

public static int fileWrite(String path, int format, String content, Object bytesObj) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.FileOutputStream;

public class Main {
    public static int fileWrite(String path, int format, String content, Object bytesObj) {
        byte[] bytes = null;
        try {//from ww  w  . ja v  a2  s .c  om
            switch (format) {
            case 0:
                bytes = (byte[]) bytesObj;
                break;
            case 1: // UTF8
                bytes = content.getBytes("UTF-8");
                break;
            case 2: // UTF8 w/ BOM
                bytes = content.getBytes("UTF-8");
                byte[] tbytes = new byte[bytes.length + 3];
                System.arraycopy(bytes, 0, tbytes, 3, bytes.length);
                bytes = tbytes;
                bytes[0] = (byte) 239;
                bytes[1] = (byte) 187;
                bytes[2] = (byte) 191;
                break;
            case 3: // UTF16
                bytes = content.getBytes("UTF-16LE");
                break;
            case 4: // UTF32
                bytes = content.getBytes("UTF-32LE");
                break;
            case 5:
                bytes = content.getBytes("ISO-8859-1");
                break;
            default:
                return 3;
            }
        } catch (java.io.UnsupportedEncodingException uee) {
            throw new RuntimeException("Unsupported encoding exception in fileWrite");
        }

        FileOutputStream stream = null;
        try {
            stream = new FileOutputStream(path);
            stream.write(bytes);
            return 0;
        } catch (java.io.IOException ioe) {
            return 1;
        } finally {
            if (stream != null) {
                try {
                    stream.close();
                } catch (java.io.IOException ioe) {
                    return 1;
                }
            }
        }
    }
}

Related

  1. fileWrite(String filename, byte[] ba)
  2. fileWrite(String fileName, String str)
  3. fileWrite(String filePath, String data)
  4. fileWrite(String filePath, String fileName, String content)
  5. fileWrite(String[] text, File file)
  6. fileWriteOut(InputStream in, String outPath)
  7. fileWriter(int startpt, int letter)
  8. fileWriter(String outfile, String contents, boolean append)