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

Source Link

Document

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

Usage

From source file:Main.java

public static void CopyFile(String source, String destination) throws IOException {
    FileInputStream fin = new FileInputStream(source);
    FileOutputStream fout = new FileOutputStream(destination);

    byte[] b = new byte[1024 * 1024];
    int noOfBytes = 0;

    while ((noOfBytes = fin.read(b)) != -1) {
        fout.write(b, 0, noOfBytes);
    }//from  ww  w.  jav  a  2  s  .c  o  m

    fin.close();
    fout.close();
}

From source file:Main.java

public static void unZip(String zipFile, String outputFolder) {
    byte[] buffer = new byte[BUFFER_SIZE];

    try {/*from w w w .  ja v  a 2 s .  c  om*/
        File folder = new File(outputFolder);
        if (!folder.exists()) {
            folder.mkdir();
        }

        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(outputFolder + File.separator + fileName);

            System.out.println("file unzip : " + newFile.getAbsoluteFile());
            if (ze.isDirectory()) {
                newFile.mkdirs();
            } else {
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
            }
            ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
        System.out.println("Done");
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

/**
 * Reads the in_stream and extracts them to out_dir.
 * @param in_stream Input stream corresponding to the zip file.
 * @param out_dir Output directory for the zip file contents.
 * @throws IOException//www  .j a  va2  s.  c o  m
 */
public static void zipExtract(InputStream in_stream, File out_dir) throws IOException {
    if (!out_dir.exists()) {
        if (!out_dir.mkdirs()) {
            throw new IOException("Could not create output directory: " + out_dir.getAbsolutePath());
        }
    }
    ZipInputStream zis = new ZipInputStream(in_stream);
    ZipEntry ze;
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            File fmd = new File(out_dir.getAbsolutePath() + "/" + ze.getName());
            fmd.mkdirs();
            continue;
        }
        FileOutputStream fout = new FileOutputStream(out_dir.getAbsolutePath() + "/" + ze.getName());
        while ((count = zis.read(buffer)) != -1) {
            fout.write(buffer, 0, count);
        }

        fout.close();
        zis.closeEntry();
    }
    zis.close();
}

From source file:Main.java

private static void movefile(String srcPath, String dstPath) throws IOException {
    File srcFile = new File(srcPath);
    File dstFile = new File(dstPath);

    if (srcFile.exists()) {
        FileInputStream inputStream = new FileInputStream(srcFile);
        FileOutputStream outputStream = new FileOutputStream(dstFile);
        byte[] bytes = new byte[1024];
        int reads = 0;
        while ((reads = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, reads);
        }/*from  ww w.j av  a  2s.  c  o  m*/
        srcFile.delete();
    }

}

From source file:Main.java

/**
 * Copy file a to b and remove a. // ww  w  . j a  v  a2s . co  m
 * @param source source file
 * @param destination destination file
 * @return true if success, false if fail.
 */
public static boolean move(File source, File destination) {
    try {
        FileInputStream in = new FileInputStream(source);
        FileOutputStream out = new FileOutputStream(destination);
        byte[] buffer = new byte[1024 * 1024];
        int size;
        while ((size = in.read(buffer)) >= 0) {
            out.write(buffer, 0, size);
        }
        in.close();
        out.flush();
        out.close();
        source.delete();
        return true;
    } catch (Exception exp) {
        exp.printStackTrace();
    }
    return false;
}

From source file:Main.java

/**
 * Copy file from oldPath to newPath/*from w  ww. j  a va2  s.co  m*/
 *
 * @param oldPath
 * @param newPath
 */
public static void copyFile(String oldPath, String newPath) {
    try {
        int byteread = 0;
        File oldfile = new File(oldPath);
        File newfile = new File(newPath);
        if (newfile.exists()) {
            newfile.delete();
        }
        newfile.createNewFile();
        if (oldfile.exists()) {
            FileInputStream inStream = new FileInputStream(oldPath);
            FileOutputStream outStream = new FileOutputStream(newPath);
            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, byteread);
            }
            inStream.close();
            outStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void copyFile(File fromFile, File toFile, Boolean rewrite) {
    if (!fromFile.exists()) {
        return;//  w w w  . ja  va  2s.  co  m
    }
    if (!fromFile.isFile()) {
        return;
    }
    if (!fromFile.canRead()) {
        return;
    }
    if (!toFile.getParentFile().exists()) {
        toFile.getParentFile().mkdirs();
    }
    if (toFile.exists() && rewrite) {
        toFile.delete();
    }

    try {
        java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile);
        java.io.FileOutputStream fosto = new FileOutputStream(toFile);
        byte bt[] = new byte[1024];
        int c;
        while ((c = fosfrom.read(bt)) > 0) {
            fosto.write(bt, 0, c);
        }
        fosfrom.close();
        fosto.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static boolean writeStringToFile(String fileName, String content) {
    File file = new File(fileName);
    if (content == null)
        return false;

    if (!file.exists()) {
        String filepath = file.getAbsolutePath();
        String path = getParentPath(filepath);
        File dir = new File(path);
        dir.mkdirs();// www.  jav a2s .  co  m
    }
    FileOutputStream fos = null;
    byte[] data;
    try {
        fos = new FileOutputStream(file, false);
        data = content.getBytes();
        fos.write(data, 0, data.length);
        fos.flush();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (null != fos) {
                fos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        data = null;
        fos = null;
    }
    return false;
}

From source file:Main.java

public static void unZip(InputStream zipFileIS, String outputFolder) throws IOException {

    byte[] buffer = new byte[1024];

    //create output directory is not exists
    File folder = new File(outputFolder);
    if (!folder.exists()) {
        folder.mkdir();/*w  w  w  .jav a  2 s . c  om*/
    }

    //get the zip file content
    ZipInputStream zis = new ZipInputStream(zipFileIS);
    //get the zipped file list entry
    ZipEntry ze = zis.getNextEntry();

    while (ze != null) {

        String fileName = ze.getName();
        File newFile = new File(outputFolder + File.separator + fileName);

        if (ze.isDirectory()) {
            newFile.mkdirs();
        } else {

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

            fos.flush();
            fos.close();
        }
        ze = zis.getNextEntry();
    }

    zis.closeEntry();
    zis.close();
}

From source file:Main.java

public static void divideFile() throws IOException {
    String path = "F:/ar/";
    String base = "phoneAttribution";
    String ext = ".db";

    int split = 800 * 1024;
    byte[] buf = new byte[1024];
    int num = 1;/*from   www .  ja v  a2  s  . c  om*/

    File inFile = new File(path + base + ext);
    FileInputStream fis = new FileInputStream(inFile);
    while (true) {
        FileOutputStream fos = new FileOutputStream(new File(path + base + num + ext));
        for (int i = 0; i < split / buf.length; i++) {
            int read = fis.read(buf);
            fos.write(buf, 0, buf.length);
            if (read < buf.length) {
                fis.close();
                fos.close();
                return;
            }
        }
        fos.close();
        num++;
    }
}