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 copyFolder(String oldPath, String newPath) {

    try {/*from w ww.  j ava  2  s. com*/
        (new File(newPath)).mkdirs();
        File a = new File(oldPath);
        String[] file = a.list();
        File temp = null;
        for (int i = 0; i < file.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + file[i]);
            } else {
                temp = new File(oldPath + File.separator + file[i]);
            }

            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(newPath + "/" + (temp.getName()).toString());
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = input.read(b)) != -1) {
                    output.write(b, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {
                copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "copyFolder() copy dir error", e);
    }

}

From source file:DBMS.UpdateFileUpload.java

public static boolean processFile(String path, FileItemStream item, int id) {
    try {/*from www  . j  a  va2 s  .c  o m*/
        String check = item.getName();
        if (check.endsWith(".jpg") || check.endsWith(".JPG")) {
            String imstring = "images/" + Integer.toString(id);
            File f = new File(path + File.separator + imstring);
            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();
            String dbimage = imstring + "/a.jpg";
            //dc.enterImage(dbimage);
            //im =dbimage;
            //System.out.println("Resizing!");
            //Resize rz = new Resize();
            //rz.resize(dbimage);
            BufferedImage originalImage = ImageIO.read(savedFile);
            int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
            BufferedImage resizeImageJpg = resizeImage(originalImage, type);
            ImageIO.write(resizeImageJpg, "jpg", savedFile);
            File rFile = new File(f.getAbsolutePath() + "/a.jpg");
            savedFile.renameTo(rFile);
            ProfileEditDB dc = new ProfileEditDB();
            dc.enterImage(id, dbimage);
            System.out.println("Link Entered to Database!");
            return true;
        }

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

From source file:Main.java

public static void copyInputToFile(InputStream in, String path) {
    BufferedInputStream bis = null;
    FileOutputStream fos = null;
    try {/*from  w  w  w. j  a va 2  s.  c  o m*/
        byte[] buffer = new byte[10 * 1024];
        bis = new BufferedInputStream(in);
        fos = new FileOutputStream(path);
        int a = bis.read(buffer, 0, buffer.length);
        while (a != -1) {
            fos.write(buffer, 0, a);
            fos.flush();
            a = bis.read(buffer, 0, buffer.length);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        closeOutputStream(fos);
        closeInputStream(bis);
        closeInputStream(in);
    }
}

From source file:Main.java

/**
 * Write a string value to the specified file.
 * //from www. j a va  2 s  .c o m
 * @param filename The filename
 * @param value The value
 */
public static void writeValue(String filename, Boolean value) {
    FileOutputStream fos = null;
    String sEnvia;
    try {
        fos = new FileOutputStream(new File(filename), false);
        if (value)
            sEnvia = "1";
        else
            sEnvia = "0";
        fos.write(sEnvia.getBytes());
        fos.flush();
        // fos.getFD().sync();
    } catch (FileNotFoundException ex) {
        Log.w(TAG, "file " + filename + " not found: " + ex);
    } catch (SyncFailedException ex) {
        Log.w(TAG, "file " + filename + " sync failed: " + ex);
    } catch (IOException ex) {
        Log.w(TAG, "IOException trying to sync " + filename + ": " + ex);
    } catch (RuntimeException ex) {
        Log.w(TAG, "exception while syncing file: ", ex);
    } finally {
        if (fos != null) {
            try {
                Log.w(TAG_WRITE, "file " + filename + ": " + value);
                fos.close();
            } catch (IOException ex) {
                Log.w(TAG, "IOException while closing synced file: ", ex);
            } catch (RuntimeException ex) {
                Log.w(TAG, "exception while closing file: ", ex);
            }
        }
    }
}

From source file:Main.java

public static boolean fileCopy(String from, String to) {
    boolean result = false;

    int size = 1 * 1024;

    FileInputStream in = null;/*  w w  w.  ja  va2  s.  co m*/
    FileOutputStream out = null;
    try {
        in = new FileInputStream(from);
        out = new FileOutputStream(to);
        byte[] buffer = new byte[size];
        int bytesRead = -1;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
        result = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
        }
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
        }
    }
    return result;
}

From source file:org.jboss.as.test.integration.web.annotationsmodule.WebModuleDeploymentTestCase.java

private static void createTestModule(File testModuleRoot) throws IOException {
    if (testModuleRoot.exists()) {
        throw new IllegalArgumentException(testModuleRoot + " already exists");
    }//from w w w  .ja v  a 2  s  .c o  m
    File file = new File(testModuleRoot, "main");
    if (!file.mkdirs()) {
        throw new IllegalArgumentException("Could not create " + file);
    }

    URL url = WebModuleDeploymentTestCase.class.getResource("module.xml");
    if (url == null) {
        throw new IllegalStateException("Could not find module.xml");
    }
    copyFile(new File(file, "module.xml"), url.openStream());

    JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "webTest.jar");
    jar.addClasses(ModuleServlet.class);

    Indexer indexer = new Indexer();
    try (final InputStream resource = ModuleServlet.class
            .getResourceAsStream(ModuleServlet.class.getSimpleName() + ".class")) {
        indexer.index(resource);
    }
    Index index = indexer.complete();
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    IndexWriter writer = new IndexWriter(data);
    writer.write(index);
    jar.addAsManifestResource(new ByteArrayAsset(data.toByteArray()), "jandex.idx");
    FileOutputStream jarFile = new FileOutputStream(new File(file, "webTest.jar"));
    try {
        jar.as(ZipExporter.class).exportTo(jarFile);
    } finally {
        jarFile.flush();
        jarFile.close();
    }

}

From source file:Main.java

public static File getFileFromCacheOrURL(File cacheDir, URL url) throws IOException {
    Log.i(TAG, "getFileFromCacheOrURL(): url = " + url.toExternalForm());

    String filename = url.getFile();
    int lastSlashPos = filename.lastIndexOf('/');
    String fileNameNoPath = new String(lastSlashPos == -1 ? filename : filename.substring(lastSlashPos + 1));

    File file = new File(cacheDir, fileNameNoPath);

    if (file.exists()) {
        if (file.length() > 0) {
            Log.i(TAG, "File exists in cache as: " + file.getAbsolutePath());
            return file;
        } else {//from  ww w. j a  va2 s. c  om
            Log.i(TAG, "Deleting zero length file " + file.getAbsolutePath());
            file.delete();
        }
    }

    Log.i(TAG, "File " + file.getAbsolutePath() + " does not exists.");

    URLConnection ucon = url.openConnection();
    ucon.setReadTimeout(5000);
    ucon.setConnectTimeout(30000);

    InputStream is = ucon.getInputStream();
    BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
    FileOutputStream outStream = new FileOutputStream(file);
    byte[] buff = new byte[5 * 1024];

    // Read bytes (and store them) until there is nothing more to read(-1)
    int len;
    while ((len = inStream.read(buff)) != -1) {
        outStream.write(buff, 0, len);
    }

    // Clean up
    outStream.flush();
    outStream.close();
    inStream.close();
    return file;
}

From source file:Main.java

public static boolean savePngScreenshot(Activity activity, String fileName, Bitmap screenshot, boolean sdcard) {
    try {/* w  ww . j  a va 2 s . co m*/
        FileOutputStream fos = null;
        if (!sdcard) {
            fos = activity.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
        } else {
            File f = new File(fileName);
            f.createNewFile();
            fos = new FileOutputStream(f);
        }
        screenshot.compress(Bitmap.CompressFormat.PNG, 70, fos);
        fos.flush();
        fos.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static boolean saveScreenshot(Activity activity, String fileName, Bitmap screenshot, boolean sdcard) {
    try {// w ww .j a v a2  s .c o m
        FileOutputStream fos = null;
        if (!sdcard) {
            fos = activity.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
        } else {
            File f = new File(fileName);
            f.createNewFile();
            fos = new FileOutputStream(f);
        }
        screenshot.compress(Bitmap.CompressFormat.JPEG, 70, fos);
        fos.flush();
        fos.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:net.peacesoft.nutch.crawl.ReSolrWriter.java

public static void toFile(String fileName, byte[] data) {
    FileOutputStream fos = null;
    try {//w  ww  .  j av  a  2 s  . c om
        File f = new File(fileName);
        fos = new FileOutputStream(f);
        fos.write(data);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException ex) {
    } catch (Exception ex) {
        System.out.println("Loi khi xuat ra file: " + fileName);
    } finally {
        try {
            fos.close();
        } catch (Exception ex) {
        }
    }
}