Example usage for java.io FileOutputStream flush

List of usage examples for java.io FileOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:net.jakobnielsen.imagga.upload.client.UploadClientIntegrationTest.java

public static File createTestFile() throws IOException {
    Base64 decoder = new Base64();
    byte[] imgBytes = decoder.decode(UploadClientIntegrationTest.TEST_IMAGE);
    File f = File.createTempFile("imagga", ".png");
    FileOutputStream osf = new FileOutputStream(f);
    osf.write(imgBytes);/* ww w .j  a va  2 s  . c  o  m*/
    osf.flush();
    return f;
}

From source file:Main.java

/**
 * Copy data from a source stream to destFile. Return true if succeed,
 * return false if failed.//from w w  w  .  ja v a2  s . c o m
 */
public static boolean copyToFile(InputStream inputStream, File destFile) {
    try {
        if (destFile.exists()) {
            destFile.delete();
        }
        FileOutputStream out = new FileOutputStream(destFile);
        try {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) >= 0) {
                out.write(buffer, 0, bytesRead);
            }
        } finally {
            out.flush();
            try {
                out.getFD().sync();
            } catch (IOException e) {
            }
            out.close();
        }
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:com.groupon.odo.proxylib.Utils.java

/**
 * Copies file from a resource to a local temp file
 *
 * @param sourceResource/*from  w ww .  j  a v a 2s .c om*/
 * @return Absolute filename of the temp file
 * @throws Exception
 */
public static File copyResourceToLocalFile(String sourceResource, String destFileName) throws Exception {
    try {
        Resource keystoreFile = new ClassPathResource(sourceResource);
        InputStream in = keystoreFile.getInputStream();

        File outKeyStoreFile = new File(destFileName);
        FileOutputStream fop = new FileOutputStream(outKeyStoreFile);
        byte[] buf = new byte[512];
        int num;
        while ((num = in.read(buf)) != -1) {
            fop.write(buf, 0, num);
        }
        fop.flush();
        fop.close();
        in.close();
        return outKeyStoreFile;
    } catch (IOException ioe) {
        throw new Exception("Could not copy keystore file: " + ioe.getMessage());
    }
}

From source file:com.attilax.zip.FileUtil.java

/**
 * /*w  w  w  .  j  a  va  2 s . co m*/
 * 
 * @param source     ?
 * @param target         
 * @param cache        ?
 * @throws Exception
 */
public static void mv(String source, String target, int cache) throws Exception {
    if (source.trim().equals(target.trim()))
        return;
    byte[] cached = new byte[cache];
    FileInputStream fromFile = new FileInputStream(source);
    FileOutputStream toFile = new FileOutputStream(target);
    while (fromFile.read(cached) != -1) {
        toFile.write(cached);
    }
    toFile.flush();
    toFile.close();
    fromFile.close();
    new File(source).deleteOnExit();
}

From source file:Main.java

public static void copyFile(File src, File dest) {
    if (null == src || null == dest) {
        return;/* w w w .j  a  v  a  2  s  . com*/
    }

    FileInputStream in = null;
    FileOutputStream out = null;
    try {
        in = new FileInputStream(src);
        out = new FileOutputStream(dest);
        int byteCount = 8096;
        byte[] buffer = new byte[byteCount];
        int count = 0;
        while ((count = in.read(buffer, 0, byteCount)) != -1) {
            out.write(buffer, 0, count);
        }
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.aqnote.shared.cryptology.cert.util.KeyStoreFileUtil.java

public static void writePkcsFile(String b64P12, String p12fileName) throws IOException {

    if (StringUtils.isBlank(p12fileName) || StringUtils.isBlank(b64P12)) {
        return;//  w  w w. j ava  2 s .  com
    }
    byte[] p12File = Base64.decodeBase64(b64P12);
    FileOutputStream fos = new FileOutputStream(p12fileName);
    fos.write(p12File);
    fos.flush();
    fos.close();
}

From source file:Main.java

public static boolean writeToSdcard(byte[] data, String path, String fileName) {
    FileOutputStream fos = null;
    try {/* www .  j ava  2  s .co  m*/
        File filePath = new File(path);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        File file = new File(path + fileName);
        if (file.exists()) {
            file.delete();
        }
        fos = new FileOutputStream(file);
        fos.write(data);
        fos.flush();
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static synchronized void writeToFile(String rootPath, String filename, String data) {

    File file = new File(rootPath);
    if (!file.exists()) {
        file.mkdirs();/*from   ww  w. jav a 2s.  c  o m*/

    }

    FileOutputStream fOut = null;
    try {

        File savedfile = new File(rootPath + filename);
        savedfile.createNewFile();//create file if not exists
        fOut = new FileOutputStream(savedfile, true); //append content to the end of file
        OutputStreamWriter outWriter = new OutputStreamWriter(fOut);

        outWriter.write(data);

        fOut.flush();
        outWriter.flush();

        fOut.close();
        outWriter.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

From source file:Main.java

public static void copy(String source, String target, boolean isFolder) throws Exception {
    if (isFolder) {
        (new File(target)).mkdirs();
        File a = new File(source);
        String[] file = a.list();
        File temp = null;/*w w  w.j  a v  a2  s .  com*/
        for (int i = 0; i < file.length; i++) {
            if (source.endsWith(File.separator)) {
                temp = new File(source + file[i]);
            } else {
                temp = new File(source + File.separator + file[i]);
            }
            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(target + "/" + (temp.getName()).toString());
                byte[] b = new byte[1024];
                int len;
                while ((len = input.read(b)) != -1) {
                    output.write(b, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {
                copy(source + "/" + file[i], target + "/" + file[i], true);
            }
        }
    } else {
        int byteread = 0;
        File oldfile = new File(source);
        if (oldfile.exists()) {
            InputStream inStream = new FileInputStream(source);
            File file = new File(target);
            file.getParentFile().mkdirs();
            file.createNewFile();
            FileOutputStream fs = new FileOutputStream(file);
            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
            }
            inStream.close();
            fs.close();
        }
    }
}

From source file:com.tek271.reverseProxy.servlet.ProxyFilter.java

private static MultipartEntity getMultipartEntity(HttpServletRequest request) {
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    Enumeration<String> en = request.getParameterNames();
    while (en.hasMoreElements()) {
        String name = en.nextElement();
        String value = request.getParameter(name);
        try {/*from  w ww. j  a  va2 s  . c o m*/
            if (name.equals("file")) {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                upload.setSizeMax(10000000);// 10 Mo
                List items = upload.parseRequest(request);
                Iterator itr = items.iterator();
                while (itr.hasNext()) {
                    FileItem item = (FileItem) itr.next();
                    File file = new File(item.getName());
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(item.get());
                    fos.flush();
                    fos.close();
                    entity.addPart(name, new FileBody(file, "application/zip"));
                }
            } else {
                entity.addPart(name, new StringBody(value.toString(), "text/plain", Charset.forName("UTF-8")));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } catch (FileUploadException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } catch (FileNotFoundException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }
    }

    return entity;

}