List of usage examples for java.io FileOutputStream flush
public void flush() throws IOException
From source file:at.ac.uniklu.mobile.sportal.util.Utils.java
/** * Loads a bitmap for the network or from a cached file, if it has already been downloaded before. If the file * is loaded for the first time, it is written to the cache to be available next time. * /*from w ww . ja v a2s . c o m*/ * @param context * @param sourceUrl * @param cacheFileName * @return a bitmap or null if something went wrong */ public static Bitmap downloadBitmapWithCache(Context context, String sourceUrl, String cacheFileName) throws Exception { Bitmap bitmap = null; File cacheFile = new File(context.getCacheDir(), cacheFileName); boolean cached = cacheFile.exists(); boolean cachedValid = cached && cacheFile.lastModified() > (System.currentTimeMillis() - MILLIS_PER_WEEK); if (cached && cachedValid) { // the requested file is cached, load it Log.d(TAG, "loading cached file: " + cacheFileName); bitmap = BitmapFactory.decodeFile(cacheFile.getAbsolutePath()); } else { // the requested file is not cached, download it from the network Log.d(TAG, "file " + (cachedValid ? "too old" : "not cached") + ", downloading: " + sourceUrl); byte[] data = downloadFile(sourceUrl, 3); if (data != null) { bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); if (bitmap != null) { // the download succeeded, cache the file so it is available next time FileOutputStream out = new FileOutputStream(cacheFile); out.write(data, 0, data.length); out.flush(); out.close(); } else { Log.e(TAG, "failed to download bitmap, may be an unsupported format: " + sourceUrl); } } } return bitmap; }
From source file:com.ms.commons.test.tool.ExportDatabaseData.java
private static void dealExportCmd(JdbcTemplate jdbcTemplate, ExportCmd ec) throws Exception { System.out.println("Exporting data, type: " + ec.getType()); File exportFile = getGeneFileName(ec.getType()); MemoryDatabase memoryDatabase = querySqlListToMemoryDatabase(jdbcTemplate, ec.getSqlList()); DataWriter dataWriter = DataWriterUtil.getDataWriter(ec.getType()); if (dataWriter == null) { throw new MessageException("Cannot find data writer for: " + ec.getType()); }/*from w w w . j a va 2s. co m*/ FileOutputStream fos = new FileOutputStream(exportFile); try { dataWriter.write(memoryDatabase, fos, "UTF-8"); } finally { fos.flush(); fos.close(); } System.out.println("Generated data to file: " + exportFile); }
From source file:com.example.mediastock.util.Utilities.java
/** * It saves the image into the internal storage of the app. * * @param context the context/*from w ww.j ava 2s . co m*/ * @param type the directory type: video or image * @param bitmap the image * @param id the id of the image * @return the path of the image */ public static String saveImageToInternalStorage(Context context, String type, Bitmap bitmap, int id) { ContextWrapper cw = new ContextWrapper(context.getApplicationContext()); File imageDir = cw.getDir("imageDir", Context.MODE_PRIVATE); // Create file image File file = new File(imageDir, type + id); FileOutputStream fos; try { fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } return file.getName(); }
From source file:com.example.mediastock.util.Utilities.java
/** * Method to save a media file inside the directory typeDir. It writes the bytes of the stream into the file * @param type the directory type: music, video or image * @param context the context// ww w . j a v a2s. c o m * @param inputStream the stream * @param id the id of the media file * @return the path of the media file */ public static String saveMediaToInternalStorage(String type, Context context, InputStream inputStream, int id) { ContextWrapper cw = new ContextWrapper(context.getApplicationContext()); File dir = cw.getDir(type + "Dir", Context.MODE_PRIVATE); // Create media file File file; if (type.equals(Utilities.MUSIC_DIR)) file = new File(dir, type + id + ".mp3"); else file = new File(dir, type + id); FileOutputStream fos; try { fos = new FileOutputStream(file); fos.write(Utilities.covertStreamToByte(inputStream)); fos.flush(); fos.close(); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return file.getName(); }
From source file:Main.java
public static boolean copyFile(InputStream inputStream, File target) { boolean result = false; if (target == null) { return result; }/*from ww w . j a v a2s . c om*/ FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(target); byte[] buffer = new byte[1024]; while (inputStream.read(buffer) > 0) { fileOutputStream.write(buffer); } result = true; } catch (IOException e) { e.printStackTrace(); } finally { if (fileOutputStream != null) { try { fileOutputStream.flush(); fileOutputStream.close(); } catch (IOException e) { } } } return result; }
From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java
public static void copyTest(File source, File target) throws Exception { FileInputStream fis = null;/*from www . j av a 2 s .c o m*/ FileOutputStream fos = null; try { fis = new FileInputStream(source); fos = new FileOutputStream(target); target.createNewFile(); byte[] bytes = new byte[16 * 1024]; int n = -1; while ((n = fis.read(bytes, 0, bytes.length)) > 0) { fos.write(bytes, 0, n); } fos.flush(); } finally { IOUtils.closeQuietly(fis); IOUtils.closeQuietly(fos); } }
From source file:net.ftb.util.FileUtils.java
public static void backupExtract(String zipLocation, String outputLocation) { Logger.logInfo("Extracting (Backup way)"); byte[] buffer = new byte[1024]; ZipInputStream zis = null;//from w w w.j ava2 s. co m ZipEntry ze; try { File folder = new File(outputLocation); if (!folder.exists()) { folder.mkdir(); } zis = new ZipInputStream(new FileInputStream(zipLocation)); ze = zis.getNextEntry(); while (ze != null) { File newFile = new File(outputLocation, ze.getName()); newFile.getParentFile().mkdirs(); if (!ze.isDirectory()) { FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.flush(); fos.close(); } ze = zis.getNextEntry(); } } catch (IOException ex) { Logger.logError("Error while extracting zip", ex); } finally { try { zis.closeEntry(); zis.close(); } catch (IOException e) { } } }
From source file:ZipUtil.java
public static void unzipFileToFile(File flSource, File flTarget) throws IOException { Inflater oInflate = new Inflater(false); FileInputStream stmFileIn = new FileInputStream(flSource); FileOutputStream stmFileOut = new FileOutputStream(flTarget); InflaterInputStream stmInflateIn = new InflaterInputStream(stmFileIn, oInflate); try {/* w w w.java 2s.co m*/ inflaterInputStmToFileOutputStm(stmInflateIn, stmFileOut); } //end try finally { stmFileOut.flush(); stmFileOut.close(); stmInflateIn.close(); stmFileIn.close(); } }
From source file:jBittorrentAPI.utils.IOManager.java
/** * Save the bytes in the stream to the file in parameter * @param is InputStream/*from w ww . j ava2 s .c o m*/ * @param filename String * @throws IOException */ public static void saveFromURL(InputStream is, String filename) throws IOException { FileOutputStream fos = new FileOutputStream(filename); int c; boolean noheader = true; while (true) { if ((c = is.read()) == -1) break; else { if (noheader) { fos.write(c); fos.flush(); } else { if (c == '\n' && is.read() == '\r' && is.read() == '\n') noheader = true; } } } fos.close(); }
From source file:Main.java
/** * Writes an element out to a file./* ww w .j a va 2s . co m*/ * @param element The XML element to write out. * @param fileName The file name to write to. Existing file is overwriten. */ public static void writeElement(Element element, String fileName) { File file = new File(fileName); file.delete(); DOMSource domSource = new DOMSource(element); FileOutputStream fos = null; try { fos = new FileOutputStream(file); StreamResult result = new StreamResult(fos); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(domSource, result); fos.flush(); fos.close(); } catch (Exception e) { throw new RuntimeException("Failed to write XML element to:" + fileName, e); } finally { try { fos.flush(); } catch (Exception e) { } try { fos.close(); } catch (Exception e) { } } }