List of usage examples for java.io BufferedOutputStream close
@Override public void close() throws IOException
From source file:Main.java
public static String parseObj2Xml(Object object) throws Exception { XMLEncoder encoder = null;//ww w .j a v a 2 s . c om String str = null; BufferedOutputStream bufOut = null; ByteArrayOutputStream out = null; try { out = new ByteArrayOutputStream(); bufOut = new BufferedOutputStream(out); encoder = new XMLEncoder(bufOut); encoder.writeObject(object); encoder.close(); str = new String(out.toByteArray()); } catch (Exception ex) { throw ex; } finally { if (out != null) out.close(); if (bufOut != null) bufOut.close(); } return str; }
From source file:org.openmrs.module.report.util.FileUtils.java
public static void copyFile(InputStream in, File dest) throws Exception { BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); byte[] data = new byte[2 * 1024]; int count;//from ww w. j a v a2 s.c o m while ((count = in.read(data, 0, data.length)) != -1) { out.write(data, 0, count); } out.flush(); out.close(); in.close(); }
From source file:com.kcs.core.utilities.FtpUtil.java
public static boolean getFileByStream(String hostname, int port, String username, String password, String remoteFileName, String localFileName) throws Exception { FTPClient ftp = null;// w ww . j ava 2 s .c o m boolean result = false; try { ftp = FtpUtil.openFtpConnect(hostname, port, username, password); if (null != ftp && ftp.isConnected()) { System.out.println("File has been start downloaded.............."); System.out.println("downloading file " + remoteFileName + " to " + localFileName + "."); InputStream inputStream = ftp.retrieveFileStream(remoteFileName); if (null == inputStream) { throw new Exception("File not found in server."); } else { File localFile = new File(localFileName); BufferedOutputStream bOut = new BufferedOutputStream(new FileOutputStream(localFile)); int b; long count = 0; while ((b = inputStream.read()) > -1) { bOut.write(b); count++; } bOut.close(); inputStream.close(); System.out.println("download file " + remoteFileName + "(" + count + ")bytes to " + localFileName + " done."); if (count > 0) { result = true; } } } } catch (Exception e) { e.printStackTrace(); throw new Exception("Error : File not found in server."); } finally { FtpUtil.closeFtpServer(ftp); } return result; }
From source file:com.byraphaelmedeiros.autosubdown.AutoSubDown.java
private static void downloadTo(Rule rule, String link) { LOGGER.log(Level.INFO, "Downloading " + link + "..."); try {// w ww . jav a 2 s .com String extension = FilenameUtils.getExtension(link); BufferedInputStream bis = new BufferedInputStream(new URL(link).openStream()); FileOutputStream fos = new FileOutputStream( rule.getDownloadTo() + File.separator + rule.getFileName() + "." + extension); BufferedOutputStream bos = new BufferedOutputStream(fos, 1024); byte data[] = new byte[1024]; int x = 0; while ((x = bis.read(data, 0, 1024)) >= 0) { bos.write(data, 0, x); } bos.close(); bis.close(); } catch (MalformedURLException e) { //e.printStackTrace(); LOGGER.log(Level.WARNING, "Invalid URL! (" + link + ")"); } catch (IOException e) { //e.printStackTrace(); LOGGER.log(Level.WARNING, "Unable to access the URL (" + link + ")."); } }
From source file:Main.java
public static File getFileFromBytes(byte[] b, String outputFile) { BufferedOutputStream stream = null; File file = null;/*from ww w . j av a 2s . c o m*/ try { file = new File(outputFile); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } FileOutputStream fstream = new FileOutputStream(file); stream = new BufferedOutputStream(fstream); stream.write(b); } catch (Exception e) { e.printStackTrace(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return file; }
From source file:Main.java
public static File getFileFromBytes(byte[] b, String outputFile) { BufferedOutputStream stream = null; File file = null;/*from www.j a va 2 s.c o m*/ FileOutputStream fstream = null; try { file = new File(outputFile); if (!file.exists()) { file.createNewFile(); } fstream = new FileOutputStream(file); stream = new BufferedOutputStream(fstream); stream.write(b); } catch (Exception e) { e.printStackTrace(); } if (stream != null) { try { stream.close(); fstream.close(); } catch (Exception e1) { e1.printStackTrace(); } } System.gc(); return file; }
From source file:at.asitplus.regkassen.core.base.util.CashBoxUtils.java
/** * Helper method for storing printed PDF receipts to files * * @param printedReceipts binary representation of receipts to be stored * @param prefix prefix for file names * @param baseDir base directory, where files should be written *//*w ww . j a v a 2s . c o m*/ public static void writeReceiptsToFiles(List<byte[]> printedReceipts, String prefix, File baseDir) { try { int index = 1; for (byte[] printedReceipt : printedReceipts) { ByteArrayInputStream bIn = new ByteArrayInputStream(printedReceipt); File receiptFile = new File(baseDir, prefix + "Receipt " + index + ".pdf"); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream( new FileOutputStream(receiptFile)); IOUtils.copy(bIn, bufferedOutputStream); bufferedOutputStream.close(); index++; } } catch (IOException e) { e.printStackTrace(); } }
From source file:com.dc.util.file.FileSupport.java
public static void writeBinaryFile(InputStream in, File targetFile) throws IOException { BufferedInputStream bis = null; BufferedOutputStream bos = null; try {//from w ww. j ava2s.com bis = new BufferedInputStream(in); bos = new BufferedOutputStream(new FileOutputStream(targetFile)); copyStream(bis, bos); } finally { if (bis != null) { bis.close(); } if (bos != null) { bos.close(); } } }
From source file:com.dubture.symfony.core.util.UncompressUtils.java
/** * Uncompress a tar archive entry to the specified output directory. * * @param tarInputStream The tar input stream associated with the entry * @param entry The tar archive entry to uncompress * @param outputDirectory The output directory where to put the uncompressed entry * * @throws IOException If an error occurs within the input or output stream *//* w w w . j a v a 2 s . c o m*/ public static void uncompressTarArchiveEntry(TarArchiveInputStream tarInputStream, TarArchiveEntry entry, File outputDirectory, EntryNameTranslator entryNameTranslator) throws IOException { String entryName = entryNameTranslator.translate(entry.getName()); if (entry.isDirectory()) { createDirectory(new File(outputDirectory, entryName)); return; } File outputFile = new File(outputDirectory, entryName); ensureDirectoryHierarchyExists(outputFile); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); try { IOUtils.copy(tarInputStream, outputStream); addExecutableBit(outputFile, Permissions.fromMode(entry.getMode())); } finally { outputStream.close(); } }
From source file:IORoutines.java
public static void saveStrings(File file, java.util.Collection list) throws IOException { BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(file)); try {//from ww w. j a va2s . co m PrintWriter writer = new PrintWriter(bout); java.util.Iterator i = list.iterator(); while (i.hasNext()) { String text = (String) i.next(); writer.println(text); } writer.flush(); } finally { bout.close(); } }