Example usage for java.io FileOutputStream write

List of usage examples for java.io FileOutputStream write

Introduction

In this page you can find the example usage for java.io FileOutputStream write.

Prototype

public void write(byte b[]) throws IOException 

Source Link

Document

Writes b.length bytes from the specified byte array to this file output stream.

Usage

From source file:Main.java

public synchronized static void writeCh(String path, String cmd) {
    try {/*from www  .  ja v a  2  s. c  om*/
        cmd = cmd + "\n";
        FileOutputStream out = new FileOutputStream(path, true);
        out.write(cmd.getBytes());
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String decodeToFile(File desFile, String encodedString) {
    try {/*from w  w  w. j  av  a2 s.c  om*/
        byte[] decodeBytes = Base64.decode(encodedString.getBytes(), Base64.NO_WRAP);
        FileOutputStream fos = new FileOutputStream(desFile);
        fos.write(decodeBytes);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static void writeFile(File file, byte[] b) throws IOException {
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(b);
    fos.close();/*from  www  .ja  v a2  s.  co  m*/
}

From source file:Main.java

public static void WriteStringToSD(String fileName, String content) {
    Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    File file = new File(Environment.getExternalStorageDirectory(), fileName);
    try {//w w  w .  jav a 2 s  .c o  m
        FileOutputStream fos = new FileOutputStream(file, true);
        fos.write(content.getBytes());
        fos.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void saveFile(Context context, String name, String text) throws IOException {
    FileOutputStream fos = context.openFileOutput(name, Context.MODE_PRIVATE);
    fos.write(text.getBytes());
    fos.close();//from ww w. j ava 2 s .c o  m
}

From source file:Main.java

/**
 * <pre>/*from  w w  w.ja  v a  2 s. c o  m*/
 * Save byte array to arbitrary file.
 * </pre>
 * @param in byte array
 * @param absolutePath absolute path to destination file
 */
public static void saveFile(byte[] in, String absolutePath) throws IOException {
    File file = new File(absolutePath);
    if (!file.exists()) {
        file.createNewFile();
    }

    FileOutputStream out = new FileOutputStream(absolutePath);
    out.write(in);
    out.close();
}

From source file:MainClass.java

public static void saveBinaryFile(URL u) {
    int bufferLength = 128;
    try {/*from w  w w  . j a v a2  s . com*/
        URLConnection uc = u.openConnection();
        String ct = uc.getContentType();
        int contentLength = uc.getContentLength();
        if (ct.startsWith("text/") || contentLength == -1) {
            System.err.println("This is not a binary file.");
            return;
        }

        InputStream stream = uc.getInputStream();
        byte[] buffer = new byte[contentLength];
        int bytesread = 0;
        int offset = 0;
        while (bytesread >= 0) {
            bytesread = stream.read(buffer, offset, bufferLength);
            if (bytesread == -1)
                break;
            offset += bytesread;
        }
        if (offset != contentLength) {
            System.err.println("Error: Only read " + offset + " bytes");
            System.err.println("Expected " + contentLength + " bytes");
        }

        String theFile = u.getFile();
        theFile = theFile.substring(theFile.lastIndexOf('/') + 1);
        FileOutputStream fout = new FileOutputStream(theFile);
        fout.write(buffer);
    } catch (Exception e) {
        System.err.println(e);
    }
    return;
}

From source file:Main.java

/**
 * Extract a resource into a real file//from ww w  . ja  v  a  2 s .c o m
 * 
 * @param in typically given as getResources().openRawResource(R.raw.something)
 * @param name of the resulting file
 * @param directory target directory
 * @return the resulting file
 * @throws IOException
 */
public static File extractResource(InputStream in, String filename, File directory) throws IOException {
    int n = in.available();
    byte[] buffer = new byte[n];
    in.read(buffer);
    in.close();
    File file = new File(directory, filename);
    FileOutputStream out = new FileOutputStream(file);
    out.write(buffer);
    out.close();
    return file;
}

From source file:Main.java

private static String storeImage(Context context, Bitmap bitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File f = null;//from   www.ja  v  a2s.co m
    try {
        f = File.createTempFile("citationsImg", ".png", context.getExternalCacheDir());
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return f.getAbsolutePath();
}

From source file:Main.java

public static void setCache(String rawJson) {
    try {/*from ww  w .j a  va 2  s .c  om*/
        FileOutputStream fileOutputStream = new FileOutputStream(cache);
        fileOutputStream.write(rawJson.getBytes());
        fileOutputStream.close();
    } catch (IOException e) {
        //???
        e.printStackTrace();
    }
}