InputStream reader = new BufferedInputStream( object.getObjectContent()); File file = new File("localFilename"); OutputStream writer = new BufferedOutputStream(new FileOutputStream(file)); int read = -1; while ( ( read = reader.read() ) != -1 ) { writer.write(read); } writer.flush(); writer.close(); reader.close();
/** * Write all class bytes to a file. * * @param fileName file where the bytes will be written * @param bytes bytes representing the class * @throws IOException if we fail to write the class */ public static void writeClass(String fileName, byte[] bytes) throws IOException { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName)); try { out.write(bytes); } finally { out.close(); } }
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(yourFile)); bos.write(fileBytes); bos.flush(); bos.close();
public static String encodeBinaryData( byte[] val ) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream( baos ); BufferedOutputStream bos = new BufferedOutputStream( gzos ); bos.write( val ); bos.flush(); bos.close(); return new String( Base64.encodeBase64( baos.toByteArray() ) ); }
public void chop() { File targetFile = new File(txnLogFile.getParentFile(), txnLogFile.getName() + ".chopped" + zxid); try ( InputStream is = new BufferedInputStream(new FileInputStream(txnLogFile)); OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile)) ) { if (!LogChopper.chop(is, os, zxid)) { throw new TxnLogToolkitException(ExitCode.INVALID_INVOCATION.getValue(), "Failed to chop %s", txnLogFile.getName()); } } catch (Exception e) { System.out.println("Got exception: " + e.getMessage()); } }
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(); }
private static void copyFile(File source, File target) throws IOException { Log.d(TAG, "Will copy " + source + " to " + target); if (!target.createNewFile()) { throw new RuntimeException("Couldn't create file " + target); } chmodPlusR(target); BufferedInputStream is = null; BufferedOutputStream os = null; try { is = new BufferedInputStream(new FileInputStream(source)); os = new BufferedOutputStream(new FileOutputStream(target)); byte[] buffer = new byte[4096]; while (true) { int read = is.read(buffer); if (read == -1) { break; } os.write(buffer, 0, read); } } finally { try { is.close(); } catch (IOException ie) {} try { os.close(); } catch (IOException ie) {} } }
public void createFile(long sizeInBytes) throws IOException { deleteOnExit(); FileOutputStream outputStream = new FileOutputStream(this); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream); InputStream inputStream = new RandomInputStream(sizeInBytes, binaryData); try { byte[] buffer = new byte[1024]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) > -1) { bufferedOutputStream.write(buffer, 0, bytesRead); } } finally { bufferedOutputStream.close(); outputStream.close(); inputStream.close(); } }
is = new BufferedInputStream(zipFile.getInputStream(entryFile)); os = new BufferedOutputStream(new FileOutputStream(extractTo)); byte[] buffer = new byte[ShareConstants.BUFFER_SIZE]; int length = 0; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length);
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) { } } }
public static void gzip(File source, File dest, boolean deleteSource) throws IOException { byte[] buffer = new byte[2^20]; GZIPOutputStream out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(dest), 65536)){{def.setLevel(Deflater.BEST_COMPRESSION);}}; FileInputStream in = new FileInputStream(source); int l; while ((l = in.read(buffer)) > 0) out.write(buffer, 0, l); in.close(); out.finish(); out.close(); if (deleteSource && dest.exists()) source.delete(); }
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) { } } }
private void copyAttachmentFromFile(String resourceName, int attachmentId, int expectedFilesize) throws IOException { File resourceFile = new File(getClass().getResource("/attach/" + resourceName).getFile()); File attachmentFile = new File(attachmentDir, Integer.toString(attachmentId)); BufferedInputStream input = new BufferedInputStream(new FileInputStream(resourceFile)); BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(attachmentFile)); int copied = IOUtils.copy(input, output); input.close(); output.close(); Assert.assertEquals(expectedFilesize, copied); }
/** * Copy from an input stream to a file (and buffer it) and close the input stream. * <p> * It is highly recommended to use FileUtils.retryCopy whenever possible, and not use a raw `InputStream` * * @param is The input stream to copy bytes from. `is` is closed regardless of the copy result. * @param file The file to copy bytes to. Any parent directories are automatically created. * * @return The count of bytes written to the file * * @throws IOException */ public static long copyToFileAndClose(InputStream is, File file) throws IOException { file.getParentFile().mkdirs(); try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { final long result = ByteStreams.copy(is, os); // Workarround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf os.flush(); return result; } finally { is.close(); } }
try ( OutputStream file = new BufferedOutputStream( new FileOutputStream( new File( targetDirectory, entry.getName() ) ) ) ) file.write( scratch, 0, read ); toCopy -= read; source.close();
public void executeAndSaveTo(File file) throws IOException { OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(file)); executeAndSaveTo(os); } finally { StreamUtil.closeQuietly(os); } }
public void downloadFile(String url, String toFile) throws IOException { HttpURLConnection conn = createConnection(url); InputStream iStream = fetch(conn, false); int size = 8 * 1024; BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(toFile), size); InputStream in = new BufferedInputStream(iStream, size); try { byte[] buffer = new byte[size]; int numRead; while ((numRead = in.read(buffer)) != -1) { writer.write(buffer, 0, numRead); } } finally { Helper.close(iStream); Helper.close(writer); Helper.close(in); } }
continue; BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); BufferedOutputStream bos = null; try { fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos, TypedValue.BUFFER_SIZE); while ((len = bis.read(buf, 0, TypedValue.BUFFER_SIZE)) != -1) { fos.write(buf, 0, len); bos.flush(); bos.close(); bis.close();
/** * Replaces file with given body. * * @param file File to replace. * @param body New body for the file. * @throws IOException Thrown in case of any errors. */ private void replaceFile(String file, String body) throws IOException { try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { out.write(body.getBytes()); } }
BufferedInputStream bis = new BufferedInputStream(entity.getContent()); String filePath = "sample.txt"; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(filePath))); int inByte; while((inByte = bis.read()) != -1) bos.write(inByte); bis.close(); bos.close();