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

public static void saveMyBitmap(Bitmap mBitmap) {
    File f = new File("/sdcard/head.png");
    try {/*from w  w w .ja  v  a2  s  .com*/
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveMyBitmap(String bitName, Bitmap mBitmap) {
    File f = new File(bitName);
    try {/*from w ww.  j a  va 2s .c  o m*/
        f.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 90, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Copy file a to b and remove a. //from  www  .  jav  a 2s .c om
 * @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

public static void WriteToFile(String name, Bitmap bitmap, String path) throws IOException {
    String fString = name.replaceAll("/", ".");

    File dir = new File("/sdcard/OneBus/user/" + path);
    if (!dir.exists()) {
        dir.mkdirs();/*from  w  ww  .  j a va2s  .  c  om*/
    }
    File f = new File("/sdcard/OneBus/user/" + path + "/" + fString + ".jpg");
    f.createNewFile();
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

@SuppressLint("NewApi")
public static void getURL(String path) {
    String fileName = "";
    String dir = "/IndoorNavi/";
    File sdRoot = Environment.getExternalStorageDirectory();
    try {/*from ww w  .  j av  a2s  .c om*/
        // Open the URLConnection for reading
        URL u = new URL(path);
        // URL u = new URL("http://www.baidu.com/");
        HttpURLConnection uc = (HttpURLConnection) u.openConnection();

        int code = uc.getResponseCode();
        String response = uc.getResponseMessage();
        //System.out.println("HTTP/1.x " + code + " " + response);
        for (int j = 1;; j++) {
            String key = uc.getHeaderFieldKey(j);
            String header = uc.getHeaderField(j);
            if (!(key == null)) {
                if (key.equals("Content-Name"))
                    fileName = header;
            }
            if (header == null || key == null)
                break;
            //System.out.println(uc.getHeaderFieldKey(j) + ": " + header);
        }
        Log.i("zhr", fileName);
        //System.out.println();

        try (InputStream in = new BufferedInputStream(uc.getInputStream())) {

            // chain the InputStream to a Reader
            Reader r = new InputStreamReader(in);
            int c;
            File mapFile = new File(sdRoot, dir + fileName);
            mapFile.createNewFile();
            FileOutputStream filecon = new FileOutputStream(mapFile);
            while ((c = r.read()) != -1) {
                //System.out.print((char) c);
                filecon.write(c);
                filecon.flush();

            }
            filecon.close();
        }

    } catch (MalformedURLException ex) {
        System.err.println(path + " is not a parseable URL");
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:in.co.sneh.model.CargaExcelRuralModel.java

public static boolean processFile(String path, FileItemStream item) {
    try {//from   w  ww  . j ava 2  s . c om
        File f = new File(path + File.separator + "exceles" + File.separator);
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();
        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read(b)) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return false;
}

From source file:com.elit2.app.model.FileUpload.java

public static boolean proccessFile(String path, FileItemStream item) {
    try {/*from   ww w .  j a v  a  2s  .  co  m*/
        File f = new File(path + File.separator + "images");
        if (!f.exists()) {
            f.mkdir();
        }
        File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
        FileOutputStream fos = new FileOutputStream(savedFile);
        InputStream is = item.openStream();

        int x = 0;
        byte[] b = new byte[1024];
        while ((x = is.read()) != -1) {
            fos.write(b, 0, x);
        }
        fos.flush();
        fos.close();
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static void saveBitmap(String path, Bitmap mBitmap) {
    File f = new File(path);
    if (!f.exists())
        createDipPath(path);//ww w.  j  a  va2s. c o  m
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void writeStreamToFile(InputStream is, File file) {
    FileOutputStream fos = null;
    try {// w  w  w.  jav  a  2 s.c  o  m
        fos = new FileOutputStream(file);
        byte[] buffer = new byte[4096];
        int length = 0;
        while ((length = is.read(buffer)) != -1) {
            fos.write(buffer, 0, length);
            fos.flush();
        }
        Log.d("tg", "FileUtils writeStreamToFile successed...");
    } 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

public static void savePhotoToSDCard(Bitmap photoBitmap, String path, String photoName) {
    File dir = new File(path);
    if (!dir.exists()) {
        dir.mkdirs();//from w  w w . j ava2s .  c o  m
    }
    File photoFile = new File(path, photoName);
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(photoFile);
        if (photoBitmap != null) {
            if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
                fileOutputStream.flush();
            }
        }
    } catch (FileNotFoundException e) {
        photoFile.delete();
        e.printStackTrace();
    } catch (IOException e) {
        photoFile.delete();
        e.printStackTrace();
    } finally {
        try {
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}