public void copy(File src, File dst) throws IOException { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); }
public static void writeStringToFile(File file, String data, String encoding) throws IOException { OutputStream os = null; try { os = new FileOutputStream(file); os.write(data.getBytes(encoding)); os.flush(); } finally { if (null != os) { os.close(); } } }
/** * {@inheritDoc} */ public Sink write(Manifest manifest) throws IOException { if (manifest != null) { File target = new File(folder, JarFile.MANIFEST_NAME); if (!target.getParentFile().isDirectory() && !target.getParentFile().mkdirs()) { throw new IOException("Could not create directory: " + target.getParent()); } OutputStream outputStream = new FileOutputStream(target); try { manifest.write(outputStream); } finally { outputStream.close(); } } return this; }
public static void cpdir(File src, File dest) throws BrutException { dest.mkdirs(); File[] files = src.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; File destFile = new File(dest.getPath() + File.separatorChar + file.getName()); if (file.isDirectory()) { cpdir(file, destFile); continue; } try { InputStream in = new FileInputStream(file); OutputStream out = new FileOutputStream(destFile); IOUtils.copy(in, out); in.close(); out.close(); } catch (IOException ex) { throw new BrutException("Could not copy file: " + file, ex); } } }
public static void writeStringToFile(File file, String data, String encoding) throws IOException { OutputStream os = null; try { os = new FileOutputStream(file); os.write(data.getBytes(encoding)); } finally { if (null != os) { os.close(); } } } }
/** * Flushes any pending data and closes output file. If writing to an * OutputStream, the stream is not closed. */ public boolean finish() { if (!started) return false; boolean ok = true; started = false; try { out.write(0x3b); // GIF trailer out.flush(); if (closeStream) { out.close(); } } catch (IOException e) { ok = false; } // reset for subsequent use transIndex = 0; out = null; image = null; pixels = null; indexedPixels = null; colorTab = null; closeStream = false; firstFrame = true; return ok; }
/** * Copy the contents of the given byte array to the given OutputStream. * Closes the stream when done. * @param in the byte array to copy from * @param out the OutputStream to copy to * @throws IOException in case of I/O errors */ public static void copy(byte[] in, OutputStream out) throws IOException { Assert.notNull(in, "No input byte array specified"); Assert.notNull(out, "No OutputStream specified"); try { out.write(in); } finally { try { out.close(); } catch (IOException ex) { } } }
public static File extractToTmp(String resourcePath, String tmpPrefix, Class clazz) throws BrutException { try { InputStream in = clazz.getResourceAsStream(resourcePath); if (in == null) { throw new FileNotFoundException(resourcePath); } File fileOut = File.createTempFile(tmpPrefix, null); fileOut.deleteOnExit(); OutputStream out = new FileOutputStream(fileOut); IOUtils.copy(in, out); in.close(); out.close(); return fileOut; } catch (IOException ex) { throw new BrutException("Could not extract resource: " + resourcePath, ex); } } }
public void flush () { OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(file)); properties.storeToXML(out, null); } catch (Exception ex) { throw new RuntimeException("Error writing preferences: " + file, ex); } finally { if (out != null) try { out.close(); } catch (IOException e) { } } }
final void process(Socket clnt) throws IOException { InputStream in = new BufferedInputStream(clnt.getInputStream()); String cmd = readLine(in); logging(clnt.getInetAddress().getHostName(), new Date().toString(), cmd); while (skipLine(in) > 0){ } OutputStream out = new BufferedOutputStream(clnt.getOutputStream()); try { doReply(in, out, cmd); } catch (BadHttpRequest e) { replyError(out, e); } out.flush(); in.close(); out.close(); clnt.close(); }
// Assume block needs to be inside a Try/Catch block. String path = Environment.getExternalStorageDirectory().toString(); OutputStream fOut = null; Integer counter = 0; File file = new File(path, "FitnessGirl"+counter+".jpg"); // the File to save , append increasing numeric counter to prevent files from getting overwritten. fOut = new FileOutputStream(file); Bitmap pictureBitmap = getImageBitmap(myurl); // obtaining the Bitmap pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate fOut.flush(); // Not really required fOut.close(); // do not forget to close the stream MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
public void saveKeystore(String path) throws EsHadoopSecurityException, IOException { OutputStream stream = null; try { stream = new FileOutputStream(new File(path)); saveKeystore(stream); } finally { if (stream != null) { stream.close(); } } }
private void writeZone(File outputDir, DateTimeZoneBuilder builder, DateTimeZone tz) throws IOException { if (ZoneInfoLogger.verbose()) { System.out.println("Writing " + tz.getID()); } File file = new File(outputDir, tz.getID()); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } OutputStream out = new FileOutputStream(file); try { builder.writeTo(tz.getID(), out); } finally { out.close(); } // Test if it can be read back. InputStream in = new FileInputStream(file); DateTimeZone tz2 = DateTimeZoneBuilder.readFrom(in, tz.getID()); in.close(); if (!tz.equals(tz2)) { System.out.println("*e* Error in " + tz.getID() + ": Didn't read properly from file"); } }
private void writeFile (File outFile, byte[] bytes) { OutputStream out = null; try { out = new BufferedOutputStream(new FileOutputStream(outFile)); out.write(bytes); } catch (IOException e) { throw new RuntimeException("Couldn't write file '" + outFile.getAbsolutePath() + "'", e); } finally { if (out != null) try { out.close(); } catch (IOException e) { } } }
/** Writes the specified bytes to the file. Parent directories will be created if necessary. * @param append If false, this file will be overwritten if it exists, otherwise it will be appended. * @throw RuntimeException if this file handle represents a directory, if it is a {@link FileType#Classpath} or * FileType#Internal file, or if it could not be written. */ public void writeBytes (byte[] bytes, boolean append) { OutputStream output = write(append); try { output.write(bytes); } catch (IOException ex) { throw new RuntimeException("Error writing file: " + file + " (" + type + ")", ex); } finally { try { output.close(); } catch (IOException ignored) { } } }