List of usage examples for java.io FileOutputStream flush
public void flush() throws IOException
From source file:Main.java
public static Uri saveBitmapToSDCard(Bitmap bitmap, String title) { File appDir = new File(Environment.getExternalStorageDirectory(), "Gank"); if (!appDir.exists()) { appDir.mkdirs();// w w w . j a va 2s.c o m } String fileName = title.replace("/", "-") + "-girl.jpg"; File file = new File(appDir, fileName); FileOutputStream outputStream; try { outputStream = new FileOutputStream(file); assert bitmap != null; bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); return null; } return Uri.fromFile(file); }
From source file:MainServer.UploadServlet.java
public static void inputStream2File(InputStream is, String savePath) throws Exception { System.out.println("Save path:" + savePath); File file = new File(savePath); InputStream inputStream = is; BufferedInputStream fis = new BufferedInputStream(inputStream); FileOutputStream fos = new FileOutputStream(file); int f;//from w ww . ja v a 2 s . com while ((f = fis.read()) != -1) { fos.write(f); } fos.flush(); fos.close(); fis.close(); inputStream.close(); }
From source file:Main.java
public static void Stream2File(InputStream is, String fileName) { byte[] b = new byte[1024]; int len;//from w w w . j a v a 2s . c om FileOutputStream os = null; try { os = new FileOutputStream(new File(fileName)); while ((len = is.read(b)) != -1) { os.write(b, 0, len); os.flush(); } } catch (IOException e) { e.printStackTrace(); } finally { closeIO(is); closeIO(os); } }
From source file:Main.java
public static long write(String fileName, String data, int position) throws FileNotFoundException, IOException { boolean append = false; if (position > 0) { truncateFile(fileName, position); append = true;/* ww w . j a va2 s. c o m*/ } byte[] rawData = data.getBytes(); ByteArrayInputStream in = new ByteArrayInputStream(rawData); FileOutputStream out = new FileOutputStream(fileName, append); byte buff[] = new byte[rawData.length]; in.read(buff, 0, buff.length); out.write(buff, 0, rawData.length); out.flush(); out.close(); return data.length(); }
From source file:net.firejack.platform.core.utils.SecurityHelper.java
public static void write64(byte[] data, String name) throws IOException { FileOutputStream stream = new FileOutputStream(name); stream.write(Base64.encode(data));//w w w. j av a2 s. c om stream.flush(); stream.close(); }
From source file:Main.java
public static void saveDocument(Document dom, String file) throws TransformerException, IOException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, dom.getDoctype().getPublicId()); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dom.getDoctype().getSystemId()); DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(); FileOutputStream outputStream = null; try {/*from ww w . j a va 2 s. com*/ outputStream = new FileOutputStream(file); result.setOutputStream(outputStream); transformer.transform(source, result); outputStream.flush(); } finally { if (outputStream != null) { outputStream.close(); } } }
From source file:Main.java
public static void write(InputStream in, File file) { if (file.exists()) { file.delete();/*from w w w . jav a2 s. c o m*/ } try { file.createNewFile(); FileOutputStream out = new FileOutputStream(file); byte[] buffer = new byte[1024]; while (in.read(buffer) > -1) { out.write(buffer); } out.flush(); in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void unzip(final InputStream input, final File destFolder) { try {//from w ww.j av a 2s.co m byte[] buffer = new byte[4096]; int read; ZipInputStream is = new ZipInputStream(input); ZipEntry entry; while ((entry = is.getNextEntry()) != null) { if (!entry.isDirectory()) { String fileName = entry.getName(); File fileFolder = destFolder; int lastSep = entry.getName().lastIndexOf(File.separatorChar); if (lastSep != -1) { String dirPath = fileName.substring(0, lastSep); fileFolder = new File(fileFolder, dirPath); fileName = fileName.substring(lastSep + 1); } fileFolder.mkdirs(); File file = new File(fileFolder, fileName); FileOutputStream os = new FileOutputStream(file); while ((read = is.read(buffer)) != -1) { os.write(buffer, 0, read); } os.flush(); os.close(); } } is.close(); } catch (Exception ex) { throw new RuntimeException("Cannot unzip stream to " + destFolder.getAbsolutePath(), ex); } }
From source file:Main.java
/** * Write a string value to the specified file. * //from w ww . j a va2 s . c o m * @param filename The filename * @param value The value */ public static void writeValue(String filename, String value) { FileOutputStream fos = null; try { fos = new FileOutputStream(new File(filename), false); fos.write(value.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:net.giovannicapuano.visualnovel.Memory.java
public static boolean putLastScript(Context context, String script) { try {//from ww w. j a va 2 s . c om FileOutputStream fos = context.openFileOutput(SCRIPT, Context.MODE_PRIVATE); fos.write(script.getBytes()); fos.flush(); fos.close(); return true; } catch (IOException e) { Utils.error(e); Utils.error(context, context.getString(R.string.error_saving_game)); return false; } }