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:Main.java

private static void saveToFile(byte[] data, String path) {
    if (data == null || data.length == 0) {
        return;/*  ww  w .  ja  va2  s .com*/
    }
    if (TextUtils.isEmpty(path)) {
        return;
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(path, true);
        fos.write(data);
        fos.flush();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/**
 * DB test//w w  w .java2 s . c  om
 */
public static void runBackup(Context context) {
    File file = context.getDatabasePath("PhotoDeskHiddenFolder.db");
    int size = (int) file.length();

    String path = Environment.getExternalStorageDirectory() + "/PhotoDesk/";
    try {
        byte[] buffer = new byte[size];
        InputStream inputStream = new FileInputStream(file);
        inputStream.read(buffer);
        inputStream.close();

        File outputDBDirectory = new File(path);
        if (!outputDBDirectory.isDirectory())
            outputDBDirectory.mkdir();

        path += "test.db";

        File outputFile = new File(path);
        FileOutputStream outputStream = new FileOutputStream(outputFile);
        outputStream.write(buffer);
        outputStream.flush();
        outputStream.close();

    } catch (Exception e) {
    }
}

From source file:Main.java

public static void saveImage(Bitmap bmp, String name) {
    if (bmp != null) {
        File appDir = new File(Environment.getExternalStorageDirectory(), name);
        if (!appDir.exists()) {
            try {
                appDir.createNewFile();//from  w ww  . j ava 2s  .c o m
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        try {
            FileOutputStream fos = new FileOutputStream(appDir);
            bmp.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {

    }
}

From source file:com.netsteadfast.greenstep.util.PdfConvertUtils.java

@SuppressWarnings("unchecked")
public static List<File> toImageFiles(File pdfFile, int resolution) throws Exception {
    PDDocument document = PDDocument.loadNonSeq(pdfFile, null);
    List<PDPage> pages = document.getDocumentCatalog().getAllPages();
    File tmpDir = new File(Constants.getWorkTmpDir() + "/" + PdfConvertUtils.class.getSimpleName() + "/"
            + System.currentTimeMillis() + "/");
    FileUtils.forceMkdir(tmpDir);//from   w ww . j a  v a  2 s .  co m
    List<File> files = new LinkedList<File>();
    int len = String.valueOf(pages.size() + 1).length();
    for (int i = 0; i < pages.size(); i++) {
        String name = StringUtils.leftPad(String.valueOf(i + 1), len, "0");
        BufferedImage bufImage = pages.get(i).convertToImage(BufferedImage.TYPE_INT_RGB, resolution);
        File imageFile = new File(tmpDir.getPath() + "/" + name + ".png");
        FileOutputStream fos = new FileOutputStream(imageFile);
        ImageIOUtil.writeImage(bufImage, "png", fos, resolution);
        fos.flush();
        fos.close();
        files.add(imageFile);
    }
    document.close();
    tmpDir = null;
    return files;
}

From source file:Main.java

public static String SaveBitmap(Bitmap bmp, String name) {
    File file = new File("mnt/sdcard/picture/");
    String path = null;// w  ww . jav  a  2  s .  co m
    if (!file.exists())
        file.mkdirs();
    try {
        path = file.getPath() + "/" + name;
        FileOutputStream fileOutputStream = new FileOutputStream(path);

        bmp.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();
        System.out.println("saveBmp is here");
    } catch (Exception e) {
        e.printStackTrace();
    }

    return path;
}

From source file:Main.java

public static void savePic2SD(Bitmap bitmap, String path, String folder) {

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        File fileDir = new File(folder);
        if (!fileDir.exists()) {
            fileDir.mkdir();/*from   ww  w . j  a v a 2 s  .c o  m*/
        }
    }

    File file = new File(path);
    try {
        FileOutputStream out = new FileOutputStream(file);

        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:subhan.portal.config.Util.java

public static void downloadFromUrl(String downloadUrl, String fileName) throws IOException {
    File root = android.os.Environment.getExternalStorageDirectory(); // path ke sdcard

    File dir = new File(root.getAbsolutePath() + "/youread"); // path ke folder

    if (dir.exists() == false) { // cek folder eksistensi
        dir.mkdirs(); // kalau belum ada, dibuat
    }//from   www .ja  v a  2s .co  m

    URL url = new URL(downloadUrl); // you can write here any link
    File file = new File(dir, fileName);

    // Open a connection to that URL. 
    URLConnection ucon = url.openConnection();

    // Define InputStreams to read from the URLConnection. 
    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is);

    // Read bytes to the Buffer until there is nothing more to read(-1).
    ByteArrayBuffer baf = new ByteArrayBuffer(5000);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }

    // Convert the Bytes read to a String. 
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(baf.toByteArray());
    fos.flush();
    fos.close();
}

From source file:Main.java

public static boolean saveBitmapToFile(Bitmap bitmap, String path) {
    File file = new File(path);
    FileOutputStream fOut;
    try {/*ww w .j a  v  a 2s .  co m*/
        fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 95, fOut);
        fOut.flush();
        fOut.close();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;

}

From source file:Main.java

/**
 * Creates a File from a Bitmap/*from   www. ja  v  a2s.  com*/
 *
 * @param bitmap to convert in a file
 *
 * @return File
 */
public static File createFileFromBitmap(Bitmap bitmap) {

    if (bitmap == null)
        return null;

    File photoFile = null;

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);
    File photoStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    if (photoStorage != null) {
        photoFile = new File(photoStorage, (System.currentTimeMillis()) + ".png");
        try {
            //f.createNewFile();
            FileOutputStream fo = new FileOutputStream(photoFile);
            fo.write(bytes.toByteArray());
            fo.flush();
            fo.close();
        } catch (IOException e) {
            Log.e(TAG, "Error saving image ", e);
        }
    }

    return photoFile;
}

From source file:Main.java

public static void savePic2SD(Bitmap bitmap, String path, String folder) {

    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        File fileDir = new File(folder);
        if (!fileDir.exists()) {
            fileDir.mkdir();//from w w w .  ja v  a 2 s.  c  o m
        }
    }

    File file = new File(path);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);

        if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}