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(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Usage

From source file:Main.java

private static final void copyInputStream(InputStream in, String fileName) throws FileNotFoundException {
    File file = new File(fileName);
    File parentFile = file.getParentFile();
    parentFile.mkdirs();//from w ww . j  a va2  s  .c o m
    System.out.println("Creating parent directory... " + parentFile.getAbsolutePath());
    byte[] buffer = new byte[1024];
    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream out = new BufferedOutputStream(fos);
    int len;
    try {
        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }
    } catch (IOException ex) {

    } finally {
        try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

From source file:Main.java

public static void copyToMemory(Context context, String srcFilePath, String dictFileName) throws IOException {
    File srcFile = new File(srcFilePath);
    if (!srcFile.exists() || srcFile.isDirectory()) {
        return;/*from   w  w w. j a  va 2s  .c  om*/
    }
    BufferedInputStream inBufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
    FileOutputStream fos = context.openFileOutput(dictFileName, 0);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] b = new byte[1024 * 4];
    int len;
    while ((len = inBufferedInputStream.read(b)) != -1) {
        bos.write(b, 0, len);
        bos.flush();
    }
    inBufferedInputStream.close();
    bos.close();
}

From source file:Main.java

public static void extractEntryContent(ZipInputStream zis, ZipEntry entry, String unzipdir)
        throws IOException, FileNotFoundException {

    String entryFileName = entry.getName();
    String entryPath = unzipdir + File.separator + entryFileName;

    createFile(entryPath);/*from w  ww .ja  v  a  2  s  . c om*/

    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entryPath));

    byte[] buffer = new byte[1024];
    int count = -1;
    while ((count = zis.read(buffer)) != -1) {
        bos.write(buffer, 0, count);
    }

    bos.close();
}

From source file:Main.java

public static void extractFile(ZipInputStream in, File outdir, String name) throws IOException {
    byte[] buffer = new byte[BUFFER_SIZE];
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(outdir, name)));
    int count = -1;
    while ((count = in.read(buffer)) != -1)
        out.write(buffer, 0, count);
    out.close();//ww  w .  j a  v a2  s. com
}

From source file:Main.java

public static void writeToFile(InputStream in, File target) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
    int count;/*from   ww w.java2s. c  om*/
    byte data[] = new byte[BUFFER_SIZE];
    while ((count = in.read(data, 0, BUFFER_SIZE)) != -1) {
        bos.write(data, 0, count);
    }
    bos.close();
}

From source file:Main.java

public static boolean copyFile2(File src, File dst) {
    FileInputStream i;//from ww w .  j  a va 2 s .  c  om
    try {
        i = new FileInputStream(src);
        BufferedInputStream in = new BufferedInputStream(i);
        FileOutputStream o = new FileOutputStream(dst);
        BufferedOutputStream out = new BufferedOutputStream(o);
        byte[] b = new byte[1024 * 5];
        int len;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        out.flush();
        in.close();
        out.close();
        o.close();
        i.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static boolean copyFile(File src, File tar) throws Exception {
    if (src.isFile()) {
        InputStream is = new FileInputStream(src);
        OutputStream op = new FileOutputStream(tar);
        BufferedInputStream bis = new BufferedInputStream(is);
        BufferedOutputStream bos = new BufferedOutputStream(op);
        byte[] bt = new byte[1024 * 8];
        int len = bis.read(bt);
        while (len != -1) {
            bos.write(bt, 0, len);
            len = bis.read(bt);//from   ww  w  . j a  va2 s .c  o m
        }
        bis.close();
        bos.close();
    }
    if (src.isDirectory()) {
        File[] f = src.listFiles();
        tar.mkdir();
        for (int i = 0; i < f.length; i++) {
            copyFile(f[i].getAbsoluteFile(), new File(tar.getAbsoluteFile() + File.separator + f[i].getName()));
        }
    }
    return true;
}

From source file:Main.java

/**
 * Method to save audio file to SD card//from   ww  w.j a v  a2s.c om
 * @param course_title, title of course
 * @param format, the file format, for example .mp3, .txt
 */
public static boolean copyToStorage(File source_file, String course_title, String format) {

    File output_file = getEmptyFileWithStructuredPath(course_title, format);

    try {
        byte[] buffer = new byte[4096];
        int bytesToHold;

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source_file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output_file));

        while ((bytesToHold = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, bytesToHold);
        }

        bos.flush();
        bos.close();

        return true;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

public static void unzipEPub(String inputZip, String destinationDirectory) throws IOException {
    int BUFFER = 2048;
    List zipFiles = new ArrayList();
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();/*from  w w w .  jav a  2s.c  o m*/

    ZipFile zipFile;
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {

        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
        String currentEntry = entry.getName();
        File destFile = new File(unzipDestinationDirectory, currentEntry);

        if (currentEntry.endsWith(".zip")) {
            zipFiles.add(destFile.getAbsolutePath());
        }

        File destinationParent = destFile.getParentFile();
        destinationParent.mkdirs();

        if (!entry.isDirectory()) {
            BufferedInputStream is = new BufferedInputStream(zipFile.getInputStream(entry));
            int currentByte;
            // buffer for writing file
            byte data[] = new byte[BUFFER];

            FileOutputStream fos = new FileOutputStream(destFile);
            BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);

            while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, currentByte);
            }
            dest.flush();
            dest.close();
            is.close();

        }

    }
    zipFile.close();

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
        String zipName = (String) iter.next();
        unzipEPub(zipName,
                destinationDirectory + File.separatorChar + zipName.substring(0, zipName.lastIndexOf(".zip")));
    }
}

From source file:MainClass.java

static void applyCipher(String inFile, String outFile, Cipher cipher) throws Exception {
    CipherInputStream in = new CipherInputStream(new FileInputStream(inFile), cipher);
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile));
    int BUFFER_SIZE = 8;
    byte[] buffer = new byte[BUFFER_SIZE];
    int numRead = 0;
    do {//ww w.j  ava2 s  . c o  m
        numRead = in.read(buffer);
        if (numRead > 0)
            out.write(buffer, 0, numRead);
    } while (numRead == 8);
}