List of usage examples for java.io OutputStream close
public void close() throws IOException
From source file:com.richtodd.android.repository.JSONUtility.java
public static void saveJSONObject(File file, JSONObject jsonObject) throws RepositoryException { try {//from w w w.j a v a 2 s .c o m String jsonString = jsonObject.toString(); OutputStream out = new FileOutputStream(file); try { Writer writer = new OutputStreamWriter(out); try { writer.write(jsonString); } finally { writer.close(); } } finally { out.close(); } } catch (FileNotFoundException ex) { throw new RepositoryException(ex); } catch (IOException ex) { throw new RepositoryException(ex); } }
From source file:com.stratio.deep.cassandra.embedded.CassandraServer.java
/** * Copies a resource from within the jar to a directory. * * @param resource/*from w ww. ja va 2 s. c o m*/ * @param directory * @throws IOException */ private static void copy(String resource, String directory) throws IOException { mkdir(directory); InputStream is = CassandraServer.class.getResourceAsStream(resource); String fileName = resource.substring(resource.lastIndexOf("/") + 1); File file = new File(directory + System.getProperty("file.separator") + fileName); OutputStream out = new FileOutputStream(file); byte buf[] = new byte[1024]; int len; while ((len = is.read(buf)) > 0) { out.write(buf, 0, len); } out.close(); is.close(); }
From source file:de.dakror.scpuller.SCPuller.java
public static void copyInputStream(InputStream is, OutputStream out) throws Exception { byte[] buffer = new byte[2048]; int len = is.read(buffer); while (len != -1) { out.write(buffer, 0, len);/*from w ww .ja v a2 s .c o m*/ len = is.read(buffer); } is.close(); out.close(); }
From source file:Main.java
public static void copy(InputStream stream, String path) throws IOException { final File file = new File(path); if (file.exists()) { file.delete();/*from w w w.jav a 2 s. c o m*/ } final File parentFile = file.getParentFile(); if (!parentFile.exists()) { //noinspection ResultOfMethodCallIgnored parentFile.mkdir(); } if (file.exists()) { return; } OutputStream myOutput = new FileOutputStream(path, true); byte[] buffer = new byte[1024]; int length; while ((length = stream.read(buffer)) >= 0) { myOutput.write(buffer, 0, length); } //Close the streams myOutput.flush(); myOutput.close(); stream.close(); }
From source file:be.i8c.sag.util.FileUtils.java
/** * Extract files from a zipped (and jar) file * * @param internalDir The directory you want to copy * @param zipFile The file that contains the content you want to copy * @param to The directory you want to copy to * @param deleteOnExit If true, delete the files once the application has closed. * @throws IOException When failed to write to a file * @throws FileNotFoundException If a file could not be found *//*from w w w.j a va 2s . c o m*/ public static void extractDirectory(String internalDir, File zipFile, File to, boolean deleteOnExit) throws IOException { try (ZipInputStream zip = new ZipInputStream(new FileInputStream(zipFile))) { while (true) { ZipEntry zipEntry = zip.getNextEntry(); if (zipEntry == null) break; if (zipEntry.getName().startsWith(internalDir) && !zipEntry.isDirectory()) { File f = createFile(new File(to, zipEntry.getName() .replace((internalDir.endsWith("/") ? internalDir : internalDir + "/"), ""))); if (deleteOnExit) f.deleteOnExit(); OutputStream bos = new FileOutputStream(f); try { IOUtils.copy(zip, bos); } finally { bos.flush(); bos.close(); } } zip.closeEntry(); } } }
From source file:com.autentia.common.util.FileSystemUtils.java
private static void copyFileLowLevel(File source, File target) throws FileNotFoundException, IOException { target.getParentFile().mkdirs();/*from ww w . j a v a2 s . c om*/ final InputStream is = new FileInputStream(source); final OutputStream os = new FileOutputStream(target); final byte[] buffer = new byte[65536]; // 64 KBytes de buffer int c; while ((c = is.read(buffer)) != -1) { os.write(buffer, 0, c); } os.close(); is.close(); }
From source file:com.klwork.common.utils.WebUtils.java
/** * ???//from w w w . ja v a 2 s. c om * @param request * @param response */ public static void VerificationCode(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); // int width = 60, height = 20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // ? Graphics g = image.getGraphics(); // ?? Random random = new Random(); // g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // g.setFont(new Font("Times New Roman", Font.PLAIN, 18)); // // g.setColor(new Color()); // g.drawRect(0,0,width-1,height-1); // ?155????? g.setColor(getRandColor(160, 200)); for (int i = 0; i < 155; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQUSTUVWXYZ0123456789"; // ????(4?) String sRand = ""; for (int i = 0; i < 4; i++) { int start = random.nextInt(base.length()); String rand = base.substring(start, start + 1); sRand = sRand.concat(rand); // ?? g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); g.drawString(rand, 13 * i + 6, 16); } // ??SESSION request.getSession().setAttribute("entrymrand", sRand); // g.dispose(); OutputStream out = response.getOutputStream(); // ? ImageIO.write(image, "JPEG", out); out.flush(); out.close(); }
From source file:Main.java
public static void assetsDataToSD(String fileOutPutName, String fileInPutName, Context context) throws IOException { InputStream myInput;// ww w .j a va 2 s. c o m File file = new File(fileOutPutName); /*if (!file.exists()) { file.createNewFile(); }else { return; }*/ OutputStream myOutput = new FileOutputStream(fileOutPutName); myInput = context.getAssets().open(fileInPutName); byte[] buffer = new byte[1024]; int length = myInput.read(buffer); while (length > 0) { myOutput.write(buffer, 0, length); length = myInput.read(buffer); } myOutput.flush(); myInput.close(); myOutput.close(); }
From source file:com.puppetlabs.geppetto.forge.util.TarUtils.java
/** * Unpack the content read from <i>source</i> into <i>targetFolder</i>. If the * <i>skipTopFolder</i> is set, then don't assume that the archive contains one * single folder and unpack the content of that folder, not including the folder * itself./* ww w . jav a 2 s . c om*/ * * @param source * The input source. Must be in <i>TAR</i> format. * @param targetFolder * The destination folder for the unpack. Not used when a <tt>fileCatcher</tt> is provided * @param skipTopFolder * Set to <code>true</code> to unpack beneath the top folder * of the archive. The archive must consist of one single folder and nothing else * in order for this to work. * @param fileCatcher * Used when specific files should be picked from the archive without writing them to disk. Can be * <tt>null</tt>. * @throws IOException */ public static void unpack(InputStream source, File targetFolder, boolean skipTopFolder, FileCatcher fileCatcher) throws IOException { String topFolderName = null; Map<File, Map<Integer, List<String>>> chmodMap = new HashMap<File, Map<Integer, List<String>>>(); TarArchiveInputStream in = new TarArchiveInputStream(source); try { TarArchiveEntry te = in.getNextTarEntry(); if (te == null) { throw new IOException("No entry in the tar file"); } do { if (te.isGlobalPaxHeader()) continue; String name = te.getName(); if (skipTopFolder) { int firstSlash = name.indexOf('/'); if (firstSlash < 0) throw new IOException("Archive doesn't contain one single folder"); String tfName = name.substring(0, firstSlash); if (topFolderName == null) topFolderName = tfName; else if (!tfName.equals(topFolderName)) throw new IOException("Archive doesn't contain one single folder"); name = name.substring(firstSlash + 1); } if (name.length() == 0) continue; String linkName = te.getLinkName(); if (linkName != null) { if (linkName.trim().equals("")) linkName = null; } if (fileCatcher != null) { if (linkName == null && !te.isDirectory() && fileCatcher.accept(name)) { if (fileCatcher.catchData(name, in)) // We're done here return; } continue; } File outFile = new File(targetFolder, name); if (linkName != null) { if (!OsUtil.link(targetFolder, name, te.getLinkName())) throw new IOException("Archive contains links but they are not supported on this platform"); } else { if (te.isDirectory()) { outFile.mkdirs(); } else { outFile.getParentFile().mkdirs(); OutputStream target = new FileOutputStream(outFile); StreamUtil.copy(in, target); target.close(); outFile.setLastModified(te.getModTime().getTime()); } registerChmodFile(chmodMap, targetFolder, Integer.valueOf(te.getMode()), name); } } while ((te = in.getNextTarEntry()) != null); } finally { StreamUtil.close(in); } chmod(chmodMap); }
From source file:FileMonitor.java
/** * Copy the source file system structure into the supplied target location. If * the source is a file, the destiniation will be created as a file; if the * source is a directory, the destination will be created as a directory. * // w ww . j av a2 s . c om * @param sourceFileOrDirectory * the file or directory whose contents are to be copied into the * target location * @param destinationFileOrDirectory * the location where the copy is to be placed; does not need to * exist, but if it does its type must match that of <code>src</code> * @return the number of files (not directories) that were copied * @throws IllegalArgumentException * if the <code>src</code> or <code>dest</code> references are * null * @throws IOException */ public static int copy(File sourceFileOrDirectory, File destinationFileOrDirectory) throws IOException { int numberOfFilesCopied = 0; if (sourceFileOrDirectory.isDirectory()) { destinationFileOrDirectory.mkdirs(); String list[] = sourceFileOrDirectory.list(); for (int i = 0; i < list.length; i++) { String dest1 = destinationFileOrDirectory.getPath() + File.separator + list[i]; String src1 = sourceFileOrDirectory.getPath() + File.separator + list[i]; numberOfFilesCopied += copy(new File(src1), new File(dest1)); } } else { InputStream fin = new FileInputStream(sourceFileOrDirectory); fin = new BufferedInputStream(fin); try { OutputStream fout = new FileOutputStream(destinationFileOrDirectory); fout = new BufferedOutputStream(fout); try { int c; while ((c = fin.read()) >= 0) { fout.write(c); } } finally { fout.close(); } } finally { fin.close(); } numberOfFilesCopied++; } return numberOfFilesCopied; }