Java FileOutputStream Write saveFile(String pathRoot, String subPath, boolean isFoler, InputStream in)

Here you can find the source of saveFile(String pathRoot, String subPath, boolean isFoler, InputStream in)

Description

save File

License

Open Source License

Declaration

public static void saveFile(String pathRoot, String subPath, boolean isFoler, InputStream in) 

Method Source Code


//package com.java2s;

import java.io.File;

import java.io.FileOutputStream;

import java.io.InputStream;

public class Main {
    public static void saveFile(String pathRoot, String subPath, boolean isFoler, InputStream in) {
        FileOutputStream fos = null;
        File file = new File(pathRoot + File.separator + subPath);

        if (file.getParentFile().exists() == false) {
            file.getParentFile().mkdirs();
        }/*from  w  w  w  . ja  v a 2  s .c o  m*/
        if (isFoler) {
            if (file.exists() == false) {
                file.mkdirs();
            }
            return;
        }

        byte[] buffer = new byte[2048];
        int len;
        try {
            if (file.exists()) {
                file.delete();
            }
            fos = new FileOutputStream(file);
            while ((len = in.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            fos.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }
}

Related

  1. saveFile(String fileName, String contents)
  2. saveFile(String name, byte[] data)
  3. saveFile(String path, byte[] content)
  4. saveFile(String pathFile, String data)
  5. saveFile(String pathname, String fileName, String contents)
  6. saveFile(String text, File saveFile)
  7. saveFileBinary(final File file, final byte[] data)
  8. SaveFileFromInputStream(InputStream in, String fileName, String path)
  9. SaveFileFromInputStream(InputStream stream, String path, String filename)