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

/**
 * Save text to certain file./*from  w  ww.  j ava 2s. c om*/
 * @param target_file The file to load the text.
 */
public static boolean saveFile(File target_file, String text) {
    boolean result = false;
    FileOutputStream fileOutputStream = null;
    try {
        fileOutputStream = new FileOutputStream(target_file);
        //Charset: UTF-8 (default)
        fileOutputStream.write(text.getBytes());
        result = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileOutputStream != null) {
            try {
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e) {
            }
        }
    }
    return result;
}

From source file:org.paxml.selenium.rc.FileServer.java

/**
 * Host a string as file content, preferrably in a system temp file. If
 * system temp file does not work, host in memory. In either case, a file is
 * readable for only once.// ww w .  j a v  a2 s  . c  o  m
 * 
 * @param content
 *            the file content
 * @return the hosted path
 */
public static String hostFileContent(String content) {
    String path;
    File file;
    try {
        File dir = new File(TMP_DIR);
        dir.mkdirs();
        file = File.createTempFile(FileServer.class.getSimpleName() + "_", ".tmp", dir);
        file.deleteOnExit();

        FileOutputStream fout = new FileOutputStream(file, false);
        try {
            fout.write(content.getBytes("UTF-8"));
            fout.flush();
        } finally {
            IOUtils.closeQuietly(fout);
        }
        path = TMP_FILE_IDENT + file.getName();

    } catch (IOException e) {
        path = IN_MEM_IDENT + UUID.randomUUID().toString() + ".txt";
        if (null != memFiles.putIfAbsent(path, content)) {
            throw new RuntimeException("Duplicated in memory fike key: " + path);
        }

    }
    if (log.isDebugEnabled()) {
        log.debug("File content held in: " + path);
    }
    return path;

}

From source file:org.meshpoint.anode.util.ModuleUtils.java

public static File getResource(URI httpUri, String filename) throws IOException {
    /* download */
    HttpClient http = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(httpUri);//from   ww  w  .j a v a2  s  .  c  o  m
    HttpEntity entity = http.execute(request).getEntity();
    InputStream in = entity.getContent();
    File destination = new File(resourceDir, filename);
    FileOutputStream out = new FileOutputStream(destination);
    byte[] buf = new byte[1024];
    int read;
    while ((read = in.read(buf)) != -1) {
        out.write(buf, 0, read);
    }
    in.close();
    out.flush();
    out.close();
    return destination;
}

From source file:Main.java

public static void saveISToFile(InputStream is, String fileName) throws IOException {
    File file = new File(fileName);
    file.getParentFile().mkdirs();//from  ww w .j  a  v a  2 s .co m
    File tempFile = new File(fileName + ".tmp");
    FileOutputStream fos = new FileOutputStream(tempFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    BufferedInputStream bis = new BufferedInputStream(is);
    byte[] barr = new byte[32768];
    int read = 0;
    while ((read = bis.read(barr)) > 0) {
        bos.write(barr, 0, read);
    }
    bis.close();
    bos.flush();
    fos.flush();
    bos.close();
    fos.close();
    file.delete();
    tempFile.renameTo(file);
}

From source file:Main.java

public static void base64ToBitmap(String base64Data, String imgName, String imgFormat) {
    byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    File myCaptureFile = new File("/sdcard/", imgName);
    FileOutputStream fos = null;
    try {//from   w  ww .j av  a  2s  .c o  m
        fos = new FileOutputStream(myCaptureFile);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block  
        e.printStackTrace();
    }
    boolean isTu = bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    if (isTu) {
        // fos.notifyAll();  
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:cn.mypandora.util.MyExcelUtil.java

/**
 * ?Excel/* w  w w .j  av  a2 s .c  o  m*/
 *
 * @param workbook   Excel
 * @param outputPath ?
 */
private static void saveExcelFile(Workbook workbook, String outputPath) {
    try {
        FileOutputStream fos = new FileOutputStream(outputPath);
        workbook.write(fos);

        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static byte[] copyFile(InputStream is, FileOutputStream os) throws IOException {
    MessageDigest digester = null;
    try {// w w  w.ja v a 2s.  c  o  m
        digester = MessageDigest.getInstance("MD5");

        byte[] buffer = new byte[16 * 1024];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
            digester.update(buffer, 0, length);
        }
        os.flush();
        os.close();

        //os.getFD().sync();
        //Log.d(TAG, "done copying");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        is.close();
        Log.d(TAG, "done copying");
    }
    if (digester != null) {
        byte[] digest = digester.digest();
        return digest;
    }

    return null;
}

From source file:controlador.Red.java

/**
 * Get de un fichero desde el Server al Cliente Local.
 * @param rutaLocal Ruta en la cual se creara el fichero.
 * @param name Nombre con el cual se busca el fichero en el server y se crea en local.
 * @return Estado de la operacion.//www  . ja  v a 2  s  .c  o m
 */
public static boolean getFile(String rutaLocal, String name) {
    try {
        url = new URL(conexion + name);
        URLConnection urlConn = url.openConnection();
        InputStream is = urlConn.getInputStream();
        FileOutputStream fos = new FileOutputStream(rutaLocal + name);
        byte[] buffer = new byte[BUFFER_LENGTH];

        int count;
        while ((count = is.read(buffer)) > 0) {
            fos.write(buffer, 0, count);
        }
        fos.flush();

        is.close();
        fos.close();

        return true;
    } catch (IOException ex) {
        System.out.println("Problema al recibir un fichero desde el Server: " + ex.getLocalizedMessage());
    }

    return false;
}

From source file:Functions.FileUpload.java

public static boolean processFile(String path, FileItemStream item, String houseId, String owner) {
    if ("".equals(item.getName())) {
        return false;
    }//w ww. j av a2  s. c  o m
    String nm = houseId + item.getName();
    System.out.println("nm =====================" + nm);
    try {
        File f = new File(path + File.separator + "images/");

        if (!f.exists()) {//den uparxei kan o fakelos opote den einai sthn database tpt!
            System.out.println("Mphke sthn if den exists");
            f.mkdir();
            File savedFile = new File(f.getAbsoluteFile() + File.separator + nm);
            FileOutputStream fos = new FileOutputStream(savedFile);
            InputStream is = item.openStream();
            int x = -1;
            byte b[] = new byte[1024];
            while ((x = is.read(b)) != -1) {
                fos.write(b, 0, x);
                //fos.write(x);
            }
            fos.flush();
            fos.close();
            database_common_functions.insertIntoBaseImage(houseId, nm);

            return true;
        } else {
            System.out.println("Mphke sthn if exists");
            if (database_common_functions.isHouseImageInDatabase(houseId, nm) == 1) {
                //overwrites current image with name nm.
                System.out.println("Mphke na kanei overwrite t arxeio");
                FileOutputStream fos = new FileOutputStream(f.getAbsoluteFile() + File.separator + nm);
                InputStream is = item.openStream();
                int x = -1;
                byte b[] = new byte[1024];
                while ((x = is.read(b)) != -1) {
                    fos.write(b, 0, x);
                    //fos.write(x);
                }
                fos.flush();
                fos.close();
                return true;
            } else {

                FileOutputStream fos = new FileOutputStream(f.getAbsoluteFile() + File.separator + nm);
                InputStream is = item.openStream();
                int x = -1;
                byte b[] = new byte[1024];
                while ((x = is.read(b)) != -1) {
                    fos.write(b, 0, x);
                    //fos.write(x);
                }
                fos.flush();
                fos.close();
                database_common_functions.insertIntoBaseImage(houseId, nm);
                return true;
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.eclipse.virgo.ide.jdt.internal.core.classpath.ServerClasspathUtils.java

/**
 * Saves the contents of the given {@link StringBuilder} into the settings file
 *///from w  w w .  j a  va 2s .  co  m
private static void saveFile(final IJavaProject project, final StringBuilder builder) {

    // Get the file from the project
    File file = new File(ServerCorePlugin.getDefault().getStateLocation().toFile(),
            project.getProject().getName() + CLASSPATH_FILE);
    FileOutputStream os = null;
    try {

        // Create a new file; override old file
        file.createNewFile();

        // Write the contents of the StringBuilder
        os = new FileOutputStream(file);
        os.write(builder.toString().getBytes("UTF-8"));
        os.flush();
    } catch (UnsupportedEncodingException e) {
        // can't happen as default UTF-8 is used
    } catch (IOException e) {
        JdtCorePlugin.log("Cannot save classpath entries to '" + file.getAbsolutePath() + "'", e);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
            }
        }
    }

}