List of usage examples for java.io OutputStream close
public void close() throws IOException
From source file:Main.java
/** * Copy a file from an Uri./* w w w . j av a 2s. c o m*/ * @param ctx Context. * @param src Source file. * @param dst Destination file. * @return {@code true} if the copy succeeded, {@code false} otherwise. */ public static boolean copyFile(Context ctx, Uri srcUri, File dst) { boolean retVal = false; try { InputStream inStream = ctx.getContentResolver().openInputStream(srcUri); try { OutputStream outStream = new FileOutputStream(dst); try { copyFile(inStream, outStream); retVal = true; } finally { outStream.close(); } } finally { inStream.close(); } } catch (IOException e) { e.printStackTrace(); } return retVal; }
From source file:Main.java
/** * Copy a file by using the file streams * /*w w w . j a v a 2 s . co m*/ * @param src the source file * @param dest the destination file * @throws IOException */ public static void copyFileUsingFileStreams(File source, File dest) throws IOException { InputStream input = null; OutputStream output = null; try { input = new FileInputStream(source); output = new FileOutputStream(dest); copyFileUsingStream(input, output); } finally { input.close(); output.close(); } }
From source file:com.usefullc.platform.common.utils.IOUtils.java
/** * //from w w w . jav a2 s. c o m * * @param content * @param fileName * @param response */ public static void download(byte[] content, String fileName, HttpServletResponse response) { try { // response response.reset(); // ??linux ? linux utf-8,windows GBK) String defaultEncoding = System.getProperty("file.encoding"); if (defaultEncoding != null && defaultEncoding.equals("UTF-8")) { response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso-8859-1")); } else { response.addHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1")); } // responseHeader response.addHeader("Content-Length", "" + content.length); response.setContentType("application/octet-stream"); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); toClient.write(content); toClient.flush(); toClient.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void copyfile(File src, File dec) { try {/*from w w w . j a va 2 s .co m*/ if (src == null || dec == null) { return; } InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dec); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
From source file:io.undertow.server.handlers.ChunkedResponseTransferCodingTestCase.java
@BeforeClass public static void setup() { final BlockingHandler blockingHandler = new BlockingHandler(); DefaultServer.setRootHandler(blockingHandler); blockingHandler.setRootHandler(new HttpHandler() { @Override/*from w ww . ja va 2 s .c o m*/ public void handleRequest(final HttpServerExchange exchange) { try { if (connection == null) { connection = exchange.getConnection(); } else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) { final OutputStream outputStream = exchange.getOutputStream(); outputStream.write("Connection not persistent".getBytes()); outputStream.close(); return; } new StringWriteChannelListener(message).setup(exchange.getResponseChannel()); } catch (IOException e) { throw new RuntimeException(e); } } }); }
From source file:Main.java
public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception { InputStream in = new ByteArrayInputStream(bytes); File destFile = new File(filePath); if (!destFile.getParentFile().exists()) { destFile.getParentFile().mkdirs(); }// w w w.jav a2s . c o m destFile.createNewFile(); OutputStream out = new FileOutputStream(destFile); byte[] cache = new byte[CACHE_SIZE]; int nRead = 0; while ((nRead = in.read(cache)) != -1) { out.write(cache, 0, nRead); out.flush(); } out.close(); in.close(); }
From source file:jease.cms.service.Imports.java
private static void copyStreamToFile(InputStream inputStream, java.io.File file) throws IOException { OutputStream outputStream = new FileOutputStream(file); IOUtils.copy(inputStream, outputStream); outputStream.close();//from www. j a va2 s .c o m }
From source file:Main.java
/** * Copy file from source to destination. * * @param source// w w w .j a v a 2s . co m * @param destination * @throws Exception */ public static void copyFile(String source, String destination) throws Exception { try { File f1 = new File(source); File f2 = new File(destination); InputStream in = new FileInputStream(f1); OutputStream out = new FileOutputStream(f2); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (Exception ex) { ex.printStackTrace(); throw ex; } }
From source file:Main.java
public static void streamToStream(InputStream is, OutputStream os) throws IOException { final byte[] buffer = new byte[256]; try {/*from w w w. ja v a2s . co m*/ int n; while ((n = is.read(buffer)) != -1) { os.write(buffer, 0, n); } } finally { os.flush(); os.close(); is.close(); } }
From source file:Main.java
/** * Writes the contents from the given input stream to the given file. * * @param in the InputStream to read from * @param outputFileName the name of the file to write to * @return the URL for the local file//from w w w. ja v a2s.co m */ public static String writeStreamToFile(InputStream in, String outputFileName) throws IOException { File file = new File(outputFileName); // Create the parent directory. file.getParentFile().mkdirs(); OutputStream out = new FileOutputStream(file); try { copy(in, out); // Return the URL to the output file. return file.toURI().toString(); } finally { out.flush(); out.close(); } }