Example usage for java.io BufferedOutputStream write

List of usage examples for java.io BufferedOutputStream write

Introduction

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

Prototype

@Override
public synchronized void write(int b) throws IOException 

Source Link

Document

Writes the specified byte to this buffered output stream.

Usage

From source file:Util.java

/**
 * Writes the specified byte[] to the specified File path.
 * /* w  w  w. jav a2s .  c o m*/
 * @param theFile File Object representing the path to write to.
 * @param bytes The byte[] of data to write to the File.
 * @throws IOException Thrown if there is problem creating or writing the 
 * File.
 */
public static void writeBytesToFile(File theFile, byte[] bytes) throws IOException {
    BufferedOutputStream bos = null;

    try {
        FileOutputStream fos = new FileOutputStream(theFile);
        bos = new BufferedOutputStream(fos);
        bos.write(bytes);
    } finally {
        if (bos != null) {
            try {
                //flush and close the BufferedOutputStream
                bos.flush();
                bos.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:Main.java

public static boolean bufferedCopyStream(InputStream inStream, OutputStream outStream, boolean closeStream)
        throws Exception {
    BufferedInputStream bis = new BufferedInputStream(inStream);
    BufferedOutputStream bos = new BufferedOutputStream(outStream);
    while (true) {
        int data = bis.read();
        if (data == -1) {
            break;
        }//from w  ww . j  a  va2s.  c o  m
        bos.write(data);
    }
    bos.flush();
    if (closeStream) {
        bos.close();
    }
    return true;
}

From source file:org.slc.sli.ingestion.IngestionTest.java

public static File createTestFile(String prefix, String suffix, String fileContents) throws IOException {
    File file = createTempFile(prefix, suffix);
    BufferedOutputStream outputStream = null;

    try {//from w w  w  .j av a2  s . c  om
        outputStream = new BufferedOutputStream(new FileOutputStream(file));
        outputStream.write(fileContents.getBytes());
    } finally {

        if (outputStream != null) {
            outputStream.close();
        }
    }

    return file;
}

From source file:Main.java

public static File getFileFromBytes(byte[] b, String outputFile) {
    File ret = null;/*from  w w  w  .  ja v  a 2s  .co  m*/
    BufferedOutputStream stream = null;
    try {
        ret = new File(outputFile);
        FileOutputStream fstream = new FileOutputStream(ret);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        // log.error("helper:get file from byte process error!");
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // log.error("helper:get file from byte process error!");
                e.printStackTrace();
            }
        }
    }
    return ret;
}

From source file:com.eryansky.common.utils.io.IoUtils.java

public static void writeStringToFile(String content, String filePath) {
    BufferedOutputStream outputStream = null;
    try {//from w  w  w  .  j  av  a 2s.  c  om
        outputStream = new BufferedOutputStream(new FileOutputStream(getFile(filePath)));
        outputStream.write(content.getBytes());
        outputStream.flush();
    } catch (Exception e) {
        //      throw new ServiceException("Couldn't write file " + filePath, e);
    } finally {
        IoUtils.closeSilently(outputStream);
    }
}

From source file:Main.java

private static File saveFile(InputStream is, String destDir, String fileName) throws IOException {
    File dir = new File(destDir);
    fileName = getString(fileName);//from ww  w . ja  va  2s . c o  m
    if (!dir.exists()) {
        dir.mkdirs();
    }
    BufferedInputStream bis = new BufferedInputStream(is);
    BufferedOutputStream bos = null;
    try {
        File saveFile = new File(destDir, fileName);
        bos = new BufferedOutputStream(new FileOutputStream(saveFile));
        int len = -1;
        while ((len = bis.read()) != -1) {
            bos.write(len);
            bos.flush();
        }
        bos.close();
        bis.close();
        return saveFile;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.atlbike.etl.service.ClientFormLogin.java

private static void saveEntity(HttpEntity loginEntity) throws IOException, FileNotFoundException {
    InputStream inStream = loginEntity.getContent();
    BufferedInputStream bis = new BufferedInputStream(inStream);
    String path = "localFile.csv";
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(path)));
    int byteCount = 0;
    int inByte;// ww  w  . ja v a  2 s .c  o  m
    while ((inByte = bis.read()) != -1) {
        bos.write(inByte);
        byteCount++;
    }
    bis.close();
    bos.close();
    System.out.println("Byte Count: " + byteCount);
}

From source file:Main.java

public static File getFileFromBytes(String name, String path) {
    byte[] b = name.getBytes();
    BufferedOutputStream stream = null;
    File file = null;/*from www . ja v  a 2 s .c  o  m*/
    try {
        file = new File(Environment.getExternalStorageDirectory() + path);
        FileOutputStream fstream = new FileOutputStream(file);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }
    return file;
}

From source file:edu.caltech.ipac.firefly.server.network.HttpServices.java

static void readBody(OutputStream os, InputStream body) {
    BufferedInputStream bis = new BufferedInputStream(body);
    BufferedOutputStream bos = new BufferedOutputStream(os);
    try {//from w  w  w  . j ava2s  .c o  m
        int b;
        while ((b = bis.read()) != -1) {
            bos.write(b);
        }

        bos.flush();

    } catch (IOException e) {
        LOG.error(e, "Error while reading response body");
    }
}

From source file:SigningProcess.java

public static void writeByteArraysToFile(String fileName, byte[] content) throws IOException {
    File file = new File(fileName);
    BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
    writer.write(content);
    writer.flush();/*from   ww w .  jav a  2  s  . c o  m*/
    writer.close();

}