List of usage examples for java.io FileOutputStream close
public void close() throws IOException
From source file:Main.java
/** * Clear old data and inserts new data into file. * @param filename//from ww w .j a v a 2 s . com * @param data * @return <c>true</c>, if data to file was inserted, <c>false</c> otherwise.</returns> */ public static boolean InsertDataToFile(String filename, String data) { // get file paht String filePath = generateFilePath(filename); try { // open file to write FileOutputStream fos = new Activity().openFileOutput(filePath, Context.MODE_PRIVATE); // write data to file fos.write(data.getBytes()); // close file fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return true; }
From source file:Main.java
public static void copyFileFromAssets(Context context, String fileName, File outputFile) { byte[] buffer = new byte[BUFFER_SIZE]; int bytesCount; try {//from w w w .j a v a 2 s . c om InputStream imageStream = context.getAssets().open(fileName); FileOutputStream fileOutputStream = new FileOutputStream(outputFile); while ((bytesCount = imageStream.read(buffer)) >= 0) { fileOutputStream.write(buffer, 0, bytesCount); } fileOutputStream.close(); imageStream.close(); } catch (IOException | Resources.NotFoundException e) { e.printStackTrace(); } }
From source file:deodex.tools.TarGzUtils.java
/** * Ungzip an input file into an output file. * <p>/* w w w. ja v a2s. co m*/ * The output file is created in the output folder, having the same name * as the input file, minus the '.gz' extension. * * @param inputFile the input .gz file * @param outputDir the output directory file. * @throws IOException * @throws FileNotFoundException * * @return The {@File} with the ungzipped content. */ public static File unGzip(final File inputFile, final File outputDir) throws FileNotFoundException, IOException { final File outputFile = new File(outputDir, inputFile.getName().substring(0, inputFile.getName().length() - 3)); final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile)); final FileOutputStream out = new FileOutputStream(outputFile); IOUtils.copy(in, out); in.close(); out.close(); return outputFile; }
From source file:Main.java
/** * Write an XML document out to a file in UTF-8 * @param document - the document to write. * @param file - the file to which the XML will be written * @param indent - the indent level.// w w w. j a v a 2 s . c om */ public static void writeXmlDocumentToFile(Document document, File file, int indent) { try { FileOutputStream fos = new FileOutputStream(file); try { writeXmlDocumentToStream(document, fos, indent); } finally { fos.close(); } } catch (IOException e) { throw new RuntimeException(e); } }
From source file:net.duckling.ddl.util.ImageUtils.java
/** * ??//from w w w . ja v a 2s. c om * @param in ? * @return ? * */ public static String saveAsFile(InputStream in) { String fileName = PATH + TEMP + System.nanoTime(); try { File directory = new File(PATH); if (!directory.exists()) { directory.mkdirs(); } File file = new File(fileName); FileOutputStream fos = new FileOutputStream(file); IOUtils.copy(in, fos); in.close(); fos.close(); } catch (IOException e) { throw new RuntimeException("save tmp file error:" + fileName, e); } return fileName; }
From source file:Main.java
private static void blockingStoreBitmap(Context context, Bitmap bitmap, String filename) { FileOutputStream fOut = null; try {//from www . j a v a 2 s . co m fOut = context.openFileOutput(filename, Context.MODE_PRIVATE); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); fOut.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fOut != null) { fOut.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:Main.java
public static void makeInternalCopy(Context c, String path, int resource) { InputStream is = c.getResources().openRawResource(resource); try {/*from ww w . ja v a 2 s. c om*/ Process process = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(process.getOutputStream()); File sniff = new File(path); if (sniff.exists()) { os.writeBytes("rm " + path + "\n"); } byte[] bytes = new byte[is.available()]; FileOutputStream setdbOutStream = new FileOutputStream(path); DataInputStream dis = new DataInputStream(is); dis.readFully(bytes); setdbOutStream.write(bytes); setdbOutStream.close(); os.writeBytes("chmod 777 " + path + "\n"); os.writeBytes("exit\n"); os.flush(); } catch (Exception e) { // Toast.makeText(c, "Error Copying file: " + e.toString(),Toast.LENGTH_SHORT).show(); e.printStackTrace(); } }
From source file:Main.java
public static void setFBImage(final String fbid, final Context context, final ImageView v) { new AsyncTask<String, Void, Bitmap>() { @Override//from w w w . ja v a2 s. co m protected Bitmap doInBackground(String... strings) { File img = new File(context.getFilesDir() + "/profile.jpg"); Bitmap bmp = null; if (img.exists()) { try { bmp = BitmapFactory.decodeFile(img.getAbsolutePath()); } catch (Exception E) { E.printStackTrace(); } } else { try { URL img_url = new URL("https://graph.facebook.com/" + String.valueOf(fbid) + "/picture?type=large&redirect=true&width=400&height=400"); bmp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream()); FileOutputStream fOut = new FileOutputStream(img); bmp.compress(Bitmap.CompressFormat.JPEG, 90, fOut); fOut.flush(); fOut.close(); } catch (Exception E) { E.printStackTrace(); } } return bmp; } @Override protected void onPostExecute(Bitmap img) { if (img != null) { v.setImageBitmap(img); } } }.execute(); }
From source file:Main.java
public static boolean copyFile(File fin, File fout) { FileInputStream in = null;//from w w w .ja v a 2 s. c o m FileOutputStream out = null; try { in = new FileInputStream(fin); out = new FileOutputStream(fout); copyFile(in, out); in.close(); out.close(); } catch (IOException e) { if (in != null) try { in.close(); } catch (IOException e2) { } if (out != null) try { out.close(); } catch (IOException e3) { } return false; } return true; }
From source file:Main.java
public static void copyFile(FileInputStream src, FileOutputStream dst) throws Throwable { byte[] buf = new byte[65536]; for (int len = src.read(buf); len > 0; len = src.read(buf)) { dst.write(buf, 0, len);/*from w ww .j a va 2s.com*/ } src.close(); dst.close(); }