List of usage examples for java.io OutputStream close
public void close() throws IOException
From source file:com.carrotgarden.eclipse.fileinstall.util.ProjectUtil.java
/** * Extract class path resource to the file system. *//* w w w . jav a 2 s . com*/ public static void copyFromClasspathIntoProject( // final Class<?> klaz, // final String sourcePath, // final IProject project, // final String targetPath // ) throws Exception { final InputStream input = klaz.getResourceAsStream(sourcePath); final File target = file(project, targetPath); if (!target.exists()) { target.getParentFile().mkdirs(); } final OutputStream output = new FileOutputStream(target); IOUtils.copy(input, output); input.close(); output.close(); }
From source file:de.nx42.maps4cim.util.Compression.java
/** * Reads the first file entry in a zip file and writes it in uncompressed * form to the desired file./*from w w w . j av a2s . com*/ * @param zipFile the zip file to read from * @param dest the file to write the first zip file entry to * @return same as destination * @throws IOException if there is an error accessing the zip file or the * destination file */ public static File readFirstZipEntry(File zipFile, File dest) throws IOException { // open zip and get first entry ZipFile zf = new ZipFile(zipFile); Enumeration<ZipArchiveEntry> entries = zf.getEntries(); ZipArchiveEntry entry = entries.nextElement(); // write to file InputStream in = zf.getInputStream(entry); OutputStream out = new FileOutputStream(dest); ByteStreams.copy(in, out); // close all streams and return the new file in.close(); out.close(); zf.close(); return dest; }
From source file:Main.java
public static boolean copyFile(InputStream sourceFile, File destFile) throws IOException { OutputStream out = new FileOutputStream(destFile); byte[] buf = new byte[4096]; int len;/*from w ww . ja v a 2 s. c om*/ while ((len = sourceFile.read(buf)) > 0) { Thread.yield(); out.write(buf, 0, len); } out.close(); return true; }
From source file:Main.java
public static void copyFile(File src, File dst) throws IOException { if (src.isDirectory()) throw new IOException("Source is a directory"); InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dst); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len;//from w w w. j a v a2 s . co m while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); }
From source file:org.jfreechart.SVGExporter.java
public static void exportChartAsSVG(JFreeChart chart, Rectangle bounds, File svgFile) throws IOException { // Get a DOMImplementation and create an XML document DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); Document document = domImpl.createDocument(null, "svg", null); // Create an instance of the SVG Generator SVGGraphics2D svgGenerator = new SVGGraphics2D(document); // draw the chart in the SVG generator chart.draw(svgGenerator, bounds);/*from www . j a v a 2s . c o m*/ // Write svg file OutputStream outputStream = new FileOutputStream(svgFile); Writer out = new OutputStreamWriter(outputStream, "UTF-8"); svgGenerator.stream(out, true /* use css */); outputStream.flush(); outputStream.close(); }
From source file:com.binil.pushnotification.ServerUtil.java
/** * Issue a POST request to the server./*from w w w . j a v a 2s. c o m*/ * * @param endpoint POST address. * @param params request parameters. * @throws java.io.IOException propagated from POST. */ private static void post(String endpoint, Map<String, Object> params) throws IOException, JSONException { URL url = new URL(endpoint); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Cache-Control", "no-cache"); urlConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8"); urlConnection.setRequestProperty("Accept-Encoding", "gzip,deflate"); urlConnection.setRequestProperty("Accept", "*/*"); urlConnection.setDoOutput(true); JSONObject json = new JSONObject(params); String body = json.toString(); urlConnection.setFixedLengthStreamingMode(body.length()); try { OutputStream os = urlConnection.getOutputStream(); os.write(body.getBytes("UTF-8")); os.close(); } catch (Exception e) { e.printStackTrace(); } finally { int status = urlConnection.getResponseCode(); String connectionMsg = urlConnection.getResponseMessage(); urlConnection.disconnect(); if (status != HttpURLConnection.HTTP_OK) { Log.wtf(TAG, connectionMsg); throw new IOException("Post failed with error code " + status); } } }
From source file:com.guye.baffle.util.OS.java
public static void cpdir(File src, File dest) throws BaffleException { dest.mkdirs();//from ww w . j a va2 s . c o m 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 BaffleException("Could not copy file: " + file, ex); } } }
From source file:Main.java
public static void saveByteData(byte[] bytes, File pictureFile) throws IOException { OutputStream output = null; try {/*from www.j a v a 2 s .c o m*/ output = new FileOutputStream(pictureFile); output.write(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (output != null) { output.close(); } } }
From source file:com.streamsets.datacollector.util.UntarUtility.java
/** * Untar an input file into an output file. The output file is created in the output folder, having the same name as * the input file, minus the '.tar' extension. * @param inputFile the input .tar file//from ww w .j a v a2 s .c om * @param outputDir the output directory file. * @throws IOException * @throws FileNotFoundException * @return The {@link List} of {@link File}s with the untared content. * @throws ArchiveException */ private static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException { LOG.info(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath())); final List<File> untaredFiles = new LinkedList<File>(); final InputStream is = new FileInputStream(inputFile); final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { final File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { LOG.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath())); if (!outputFile.exists()) { LOG.info(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath())); if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { LOG.info(String.format("Creating output file %s.", outputFile.getAbsolutePath())); final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); } untaredFiles.add(outputFile); } debInputStream.close(); return untaredFiles; }
From source file:Main.java
/** * Copy a file.//from ww w .j ava 2s. c o m * @param src Source file. * @param dst Destination file. * @return {@code true} if the copy succeeded, {@code false} otherwise. */ public static boolean copyFile(File src, File dst) { boolean retVal = false; try { InputStream inStream = new FileInputStream(src); 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; }