List of usage examples for java.io FileOutputStream close
public void close() throws IOException
From source file:Main.java
private static long copyLarge(InputStream input, File outputFile) throws IOException { byte[] buffer = new byte[16384]; long count = 0; int n = 0;/*from ww w . j a v a 2 s.c o m*/ outputFile.getParentFile().mkdirs(); FileOutputStream output = new FileOutputStream(outputFile); try { while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } } finally { output.close(); } return count; }
From source file:Main.java
public static void WriteFile(InputStream stream, String fileName) throws IOException { FileOutputStream fos = new FileOutputStream(fileName); byte[] buff = new byte[1024]; int readLen;/*ww w. ja v a2s . c om*/ do { readLen = stream.read(buff); if (readLen > 0) fos.write(buff, 0, readLen); } while (readLen > 0); fos.close(); }
From source file:ota.otaupdates.utils.Utils.java
public static void DownloadFromUrl(String download_url, String fileName) { final String TAG = "Downloader"; if (!isMainThread()) { try {/*from w w w .java 2 s . c om*/ URL url = new URL(download_url); if (!new File(DL_PATH).isDirectory()) { if (!new File(DL_PATH).mkdirs()) { Log.e(TAG, "Creating the directory " + DL_PATH + "failed"); } } File file = new File(DL_PATH + fileName); long startTine = System.currentTimeMillis(); Log.d(TAG, "Beginning download of " + url.getPath() + " to " + DL_PATH + fileName); /* * Open a connection and define Streams */ URLConnection ucon = url.openConnection(); InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); /* * Read bytes until there is nothing left */ ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert Bytes to a String */ FileOutputStream fos = new FileOutputStream(file); fos.write(baf.toByteArray()); fos.close(); Log.d(TAG, "Download finished in " + ((System.currentTimeMillis() - startTine) / 1000) + " seconds"); } catch (Exception e) { Log.e(TAG, "Error: " + e); e.printStackTrace(); } } else { Log.e(TAG, "Tried to run in Main Thread. Aborting..."); } }
From source file:bobs.mcapisignature.CertUtils.java
public static void dump(byte[] content, String file) { try {//from w w w . j a v a 2s.c om FileOutputStream fos = new FileOutputStream(file); fos.write(content); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.globalsight.util.JfreeCharUtil.java
public static void drawPieChart2D(String title, Map<String, Double> datas, File OutFile) { PieDataset dataset = buildDatas(datas); JFreeChart chart = ChartFactory.createPieChart(title, dataset, true, true, false); PiePlot plot = (PiePlot) chart.getPlot(); plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})")); chart.setBackgroundPaint(Color.white); plot.setCircular(true);// ww w . j a v a 2 s . c om TextTitle textTitle = new TextTitle(title); Font font = new Font(textTitle.getFont().getName(), Font.CENTER_BASELINE, 20); textTitle.setFont(font); chart.setTitle(textTitle); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream(OutFile); ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 640, 480, null); fos_jpg.close(); } catch (Exception e) { s_logger.error(e.getMessage(), e); } }
From source file:ca.tbcn.greenp.GreenParkingApp.java
public static void updateJsonFile(Context context, String contents) throws IOException { // write out to local file FileOutputStream fos = context.openFileOutput(JSON_FILE_NAME, Context.MODE_PRIVATE); fos.write(contents.getBytes());/*from ww w . j a va 2s . c om*/ fos.close(); }
From source file:com.BibleQuote.utils.FsUtils.java
public static boolean loadContentFromURL(String fromURL, String toFile) { try {/*from ww w . j av a 2s. c o m*/ URL url = new URL("http://bible-desktop.com/xml" + fromURL); File file = new File(toFile); /* Open a connection to that URL. */ URLConnection ucon = url.openConnection(); /* Define InputStreams to read from the URLConnection */ InputStream is = ucon.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); /* Read bytes to the Buffer until there is nothing more to read(-1) */ ByteArrayBuffer baf = new ByteArrayBuffer(50); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte) current); } /* Convert the Bytes read to a String. */ FileOutputStream fos = new FileOutputStream(file); fos.write(baf.toByteArray()); fos.close(); } catch (IOException e) { Log.e(TAG, String.format("loadContentFromURL(%1$s, %2$s)", fromURL, toFile), e); return false; } return true; }
From source file:Main.java
public static String saveImageToTemporaryDirectory(Bitmap image) { Log.d(TAG, "[AirImagePickerUtils] Entering saveImageToTemporaryDirectory"); String path = ""; FileOutputStream outputStream; try {/*from w ww . j a v a 2 s. com*/ File file = getTemporaryFile(".jpg"); outputStream = new FileOutputStream(file); outputStream.write(getJPEGRepresentationFromBitmap(image)); outputStream.close(); path = file.getAbsolutePath(); Log.d(TAG, "[AirImagePickerUtils] saveImageToTemporaryDirectory path:" + path); } catch (IOException e) { Log.e(TAG, "[AirImagePickerUtils] saveImageToTemporaryDirectory error:" + e.toString()); // Error while creating file } Log.d(TAG, "[AirImagePickerUtils] Exiting saveImageToTemporaryDirectory"); return path; }
From source file:com.globalsight.util.JfreeCharUtil.java
public static void drawPieChart3D(String title, Map<String, Double> datas, File OutFile) { PieDataset dataset = buildDatas(datas); JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, true, true, false); PiePlot3D plot = (PiePlot3D) chart.getPlot(); plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%"))); plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})")); chart.setBackgroundPaint(Color.white); plot.setForegroundAlpha(0.7f);/*w ww. jav a 2 s . com*/ plot.setCircular(true); TextTitle textTitle = new TextTitle(title); Font font = new Font(textTitle.getFont().getName(), Font.CENTER_BASELINE, 20); textTitle.setFont(font); chart.setTitle(textTitle); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream(OutFile); ChartUtilities.writeChartAsJPEG(fos_jpg, 1, chart, 640, 480, null); fos_jpg.close(); } catch (Exception e) { s_logger.error(e.getMessage(), e); } }
From source file:com.keybox.manage.util.KeyStoreUtil.java
/** * create new keystore/*w ww. j a va 2 s . c o m*/ */ private static void initializeKeyStore() { try { keyStore = KeyStore.getInstance("JCEKS"); //create keystore keyStore.load(null, KEYSTORE_PASS); //set encryption key KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); KeyStoreUtil.setSecret(KeyStoreUtil.ENCRYPTION_KEY_ALIAS, keyGenerator.generateKey().getEncoded()); //write keystore FileOutputStream fos = new FileOutputStream(keyStoreFile); keyStore.store(fos, KEYSTORE_PASS); fos.close(); } catch (Exception ex) { log.error(ex.toString(), ex); } }