List of usage examples for java.io FileOutputStream close
public void close() throws IOException
From source file:Main.java
public static String writeBitmap(byte[] data, int cameraDegree, Rect rect, Rect willTransformRect) throws IOException { File file = new File(Environment.getExternalStorageDirectory() + "/bookclip/"); file.mkdir();//from ww w . j a v a 2 s . c om String bitmapPath = file.getPath() + "/" + System.currentTimeMillis() + ".png"; // bitmap rotation, scaling, crop BitmapFactory.Options option = new BitmapFactory.Options(); option.inSampleSize = 2; Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, option); Matrix bitmapMatrix = new Matrix(); bitmapMatrix.setRotate(cameraDegree); int x = rect.left, y = rect.top, width = rect.right - rect.left, height = rect.bottom - rect.top; Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmapMatrix, false); // bitmap recycle bitmap.recycle(); Bitmap scaledBitmap = Bitmap.createScaledBitmap(rotateBitmap, willTransformRect.right, willTransformRect.bottom - willTransformRect.top, false); // rotatebitmap recycle rotateBitmap.recycle(); Bitmap cropBitmap = Bitmap.createBitmap(scaledBitmap, x, y, width, height, null, false); // scaledBitmap recycle scaledBitmap.recycle(); // file write FileOutputStream fos = new FileOutputStream(new File(bitmapPath)); cropBitmap.compress(CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); // recycle cropBitmap.recycle(); return bitmapPath; }
From source file:Main.java
/** * Output an XML document to the given file. * /*from ww w.j ava 2 s .c o m*/ * @param doc XML document to write. * @param filename File to write to. */ public static void writeXmlDocument(Document doc, String filename) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); DOMSource source = new DOMSource(doc); final FileOutputStream out = new FileOutputStream(new File(filename)); StreamResult result = new StreamResult(out); transformer.transform(source, result); out.close(); } catch (IOException e) { LOGGER.warning("Error closing stream or could open a file from writing an XML document."); e.printStackTrace(); } catch (TransformerConfigurationException e) { LOGGER.warning("TransformerConfigurationException thrown when writing XML document to a file."); e.printStackTrace(); } catch (TransformerException e) { LOGGER.warning("Transformer exception thrown when writing XML document to a file."); e.printStackTrace(); } }
From source file:Main.java
public static boolean saveFile(String filePath, String str) throws IOException { boolean result = false; if (filePath != null && str != null) { Log.d(TAG, "filePath:" + filePath); File file = new File(filePath); if (file.exists()) { file.delete();/* w w w. j a v a2s . co m*/ } if (file.createNewFile()) { FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()); fos.write(str.getBytes("gb18030")); fos.flush(); fos.close(); result = true; } } return result; }
From source file:fr.gael.dhus.util.UnZip.java
public static void unCompress(String zip_file, String output_folder) throws IOException, CompressorException, ArchiveException { ArchiveInputStream ais = null;//from w w w . j a v a 2 s .com ArchiveStreamFactory asf = new ArchiveStreamFactory(); FileInputStream fis = new FileInputStream(new File(zip_file)); if (zip_file.toLowerCase().endsWith(".tar")) { ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis); } else if (zip_file.toLowerCase().endsWith(".zip")) { ais = asf.createArchiveInputStream(ArchiveStreamFactory.ZIP, fis); } else if (zip_file.toLowerCase().endsWith(".tgz") || zip_file.toLowerCase().endsWith(".tar.gz")) { CompressorInputStream cis = new CompressorStreamFactory() .createCompressorInputStream(CompressorStreamFactory.GZIP, fis); ais = asf.createArchiveInputStream(new BufferedInputStream(cis)); } else { try { fis.close(); } catch (IOException e) { LOGGER.warn("Cannot close FileInputStream:", e); } throw new IllegalArgumentException("Format not supported: " + zip_file); } File output_file = new File(output_folder); if (!output_file.exists()) output_file.mkdirs(); // copy the existing entries ArchiveEntry nextEntry; while ((nextEntry = ais.getNextEntry()) != null) { File ftemp = new File(output_folder, nextEntry.getName()); if (nextEntry.isDirectory()) { ftemp.mkdir(); } else { FileOutputStream fos = FileUtils.openOutputStream(ftemp); IOUtils.copy(ais, fos); fos.close(); } } ais.close(); fis.close(); }
From source file:Main.java
/** * /*ww w .ja v a2 s. co m*/ * @param file */ public static void decodeFile(File file) { Bitmap bitmap = null; try { // Decode image size BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; FileInputStream fileInputStream = new FileInputStream(file); BitmapFactory.decodeStream(fileInputStream, null, options); fileInputStream.close(); int scale = 1; if (options.outHeight > 500 || options.outWidth > 500) { scale = (int) Math.pow(2, (int) Math.round( Math.log(500 / (double) Math.max(options.outHeight, options.outWidth)) / Math.log(0.5))); } // Decode with inSampleSize BitmapFactory.Options options2 = new BitmapFactory.Options(); options2.inSampleSize = scale; fileInputStream = new FileInputStream(file); bitmap = BitmapFactory.decodeStream(fileInputStream, null, options2); fileInputStream.close(); FileOutputStream output = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 90, output); output.flush(); output.close(); } catch (Exception e) { Log.e(TAG, e.getMessage()); } }
From source file:Main.java
public static void unpack(InputStream in, File targetDir) throws IOException { ZipInputStream zin = new ZipInputStream(in); ZipEntry entry;/*from w w w . j a v a2 s .co m*/ while ((entry = zin.getNextEntry()) != null) { String extractFilePath = entry.getName(); if ((extractFilePath.startsWith("/")) || (extractFilePath.startsWith("\\"))) { extractFilePath = extractFilePath.substring(1); } File extractFile = new File(new StringBuilder().append(targetDir.getPath()).append(File.separator) .append(extractFilePath).toString()); if (entry.isDirectory()) { if (!extractFile.exists()) extractFile.mkdirs(); } else { File parent = extractFile.getParentFile(); if (!parent.exists()) { parent.mkdirs(); } FileOutputStream os = new FileOutputStream(extractFile); copyFile(zin, os); os.flush(); os.close(); } } }
From source file:com.kurento.kmf.test.services.Recorder.java
public static float getPesqMos(String audio, int sampleRate) { float pesqmos = 0; try {//from w w w. j a v a2 s. c om String pesq = KurentoServicesTestHelper.getTestFilesPath() + "/bin/pesq/PESQ"; String origWav = ""; if (audio.startsWith(HTTP_TEST_FILES)) { origWav = KurentoServicesTestHelper.getTestFilesPath() + audio.replace(HTTP_TEST_FILES, ""); } else { // Download URL origWav = KurentoMediaServerManager.getWorkspace() + "/downloaded.wav"; URL url = new URL(audio); ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(origWav); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); } Shell.runAndWait(pesq, "+" + sampleRate, origWav, RECORDED_WAV); List<String> lines = FileUtils.readLines(new File(PESQ_RESULTS), "utf-8"); pesqmos = Float.parseFloat(lines.get(1).split("\t")[2].trim()); log.info("PESQMOS " + pesqmos); Shell.runAndWait("rm", PESQ_RESULTS); } catch (IOException e) { log.error("Exception recording local audio", e); } return pesqmos; }
From source file:Main.java
public static boolean writeFileByObject(String fileName, Object object) { try {/* www . ja v a2 s . co m*/ FileOutputStream fos = new FileOutputStream(fileName); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(object); oos.flush(); oos.close(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:Main.java
public static void writeToFile(Context context, String file, String content) throws IOException { FileOutputStream fos = context.openFileOutput(file, Context.MODE_PRIVATE); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); bw.write(content);//from www . j a va 2 s.c o m bw.flush(); bw.close(); fos.close(); }
From source file:Main.java
private static void writeToOutFile(File toFile, FileInputStream fosfrom) throws IOException { FileOutputStream fosto = null; fosto = new FileOutputStream(toFile); byte bt[] = new byte[1024]; int c;//from w w w . j a va 2 s. c o m while ((c = fosfrom.read(bt)) > 0) { fosto.write(bt, 0, c); } fosto.close(); }