List of usage examples for java.io OutputStream close
public void close() throws IOException
From source file:functionalTests.vfsprovider.TestProActiveProvider.java
public static void extractZip(final ZipInputStream zipStream, final File dstFile) throws IOException { ZipEntry zipEntry;/*from w w w .j a v a 2 s . com*/ while ((zipEntry = zipStream.getNextEntry()) != null) { final File dstSubFile = new File(dstFile, zipEntry.getName()); if (zipEntry.isDirectory()) { dstSubFile.mkdirs(); if (!dstSubFile.exists() || !dstSubFile.isDirectory()) throw new IOException("Could not create directory: " + dstSubFile); } else { final OutputStream os = new BufferedOutputStream(new FileOutputStream(dstSubFile)); try { int data; while ((data = zipStream.read()) != -1) os.write(data); } finally { os.close(); } } } }
From source file:Main.java
public static void copyFile(InputStream in, File dest) throws IOException { if (!dest.exists()) { dest.createNewFile();/*from w w w . j a va 2 s . c om*/ } OutputStream out = null; try { out = new FileOutputStream(dest); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } }
From source file:com.appeligo.lucene.DidYouMeanIndexer.java
public static void createDefaultSpellIndex(String indexDir, String spellDir) throws IOException { String newSpellDir = spellDir + ".new"; File newSpellDirFile = new File(newSpellDir); if (newSpellDirFile.exists()) { String[] dirFiles = newSpellDirFile.list(); for (String dirFile : dirFiles) { File f = new File(newSpellDirFile, dirFile); if (!f.delete()) { throw new IOException("Could not delete " + f.getAbsolutePath()); }//from ww w.j av a2 s . c o m } if (!newSpellDirFile.delete()) { throw new IOException("Could not delete " + newSpellDirFile.getAbsolutePath()); } } /* This was for the original programIndex, but we found out that stemming was bad, and you get better * spelling suggestions if you can specify a single field, so we combined them. for (String field : new String[]{"text","description","programTitle", "episodeTitle", "credits", "genre"}) { createSpellIndex(field, FSDirectory.getDirectory(indexDir), FSDirectory.getDirectory(newSpellDir)); } */ createSpellIndex("compositeField", FSDirectory.getDirectory(indexDir), FSDirectory.getDirectory(newSpellDir)); String oldSpellDir = spellDir + ".old"; File oldSpellDirFile = new File(oldSpellDir); if (oldSpellDirFile.exists()) { String[] dirFiles = oldSpellDirFile.list(); for (String dirFile : dirFiles) { File f = new File(oldSpellDirFile, dirFile); if (!f.delete()) { throw new IOException("Could not delete " + f.getAbsolutePath()); } } if (!oldSpellDirFile.delete()) { throw new IOException("Could not delete " + oldSpellDirFile.getAbsolutePath()); } } File spellDirFile = new File(spellDir); if (spellDirFile.exists() && !spellDirFile.renameTo(oldSpellDirFile)) { throw new IOException("could not rename " + spellDirFile.getAbsolutePath() + " to " + oldSpellDirFile.getAbsolutePath()); } /* there is some small risk here that someone might try to get the spell index when the file isn't there yet */ /* I don't know of any way to really synchronize that from this class, and the risk is minor, unlikely, and not catastrophic */ spellDirFile = new File(spellDir); if (!newSpellDirFile.renameTo(spellDirFile)) { // What really bugs me is you can't close a SpellChecker, and I think that prevents us from renaming // the spell index directory (at least on Windows Vista), so let's copy the files instead /* throw new IOException("could not rename "+newSpellDirFile.getAbsolutePath()+" to "+spellDirFile.getAbsolutePath()); */ if (!spellDirFile.mkdir()) { throw new IOException("Couldn't make directory " + spellDirFile.getAbsolutePath()); } String[] dirFiles = newSpellDirFile.list(); for (String dirFile : dirFiles) { File f = new File(newSpellDirFile, dirFile); File toF = new File(spellDirFile, dirFile); InputStream is = new BufferedInputStream(new FileInputStream(f.getAbsolutePath())); OutputStream os = new BufferedOutputStream(new FileOutputStream(toF.getAbsolutePath())); int b; while ((b = is.read()) != -1) { os.write(b); } is.close(); os.close(); /* I'd like to do this, but the same reason the rename won't work is why this * won't work... this current program still has one or more of the files open. if (!f.delete()) { throw new IOException("Could not delete "+f.getAbsolutePath()); } } if (!newSpellDirFile.delete()) { throw new IOException("Could not delete "+newSpellDirFile.getAbsolutePath()); } */ } } }
From source file:com.zb.app.common.file.FileUtils.java
public static void unJar(File jarFile, File toDir) throws IOException { JarFile jar = new JarFile(jarFile); try {/*from w w w . j ava 2 s . c o m*/ Enumeration<JarEntry> entries = jar.entries(); while (entries.hasMoreElements()) { JarEntry entry = (JarEntry) entries.nextElement(); if (!entry.isDirectory()) { InputStream in = jar.getInputStream(entry); try { File file = new File(toDir, entry.getName()); if (!file.getParentFile().mkdirs()) { if (!file.getParentFile().isDirectory()) { throw new IOException("Mkdirs failed to create " + file.getParentFile().toString()); } } OutputStream out = new FileOutputStream(file); try { byte[] buffer = new byte[8192]; int i; while ((i = in.read(buffer)) != -1) { out.write(buffer, 0, i); } } finally { out.close(); } } finally { in.close(); } } } } finally { jar.close(); } }
From source file:com.asakusafw.shafu.core.util.IoUtils.java
private static File createFile(File base, ArchiveEntry entry, InputStream contents) throws IOException { File file = new File(base, entry.getName()); File parent = file.getParentFile(); parent.mkdirs();// ww w . j av a 2 s. co m OutputStream output = new FileOutputStream(file); try { IOUtils.copy(contents, output); } finally { output.close(); } return file; }
From source file:net.alteiar.utils.file.SerializableFile.java
/** * // w w w. ja va 2 s .c o m * @param data * @param dest * - the file must exist * @throws IOException */ private static void writeByteToFile(byte[] data, File dest) throws IOException { OutputStream os = null; IOException ex = null; try { os = new FileOutputStream(dest); os.write(data); os.flush(); } catch (IOException e) { ex = e; } finally { try { os.close(); } catch (IOException e) { ex = e; } } if (ex != null) { throw ex; } }
From source file:com.dc.util.file.FileSupport.java
public static void copy(byte[] bytes, File destinationFile) throws IOException { OutputStream out = null; try {//from ww w. j a va 2 s.c o m out = new BufferedOutputStream(new FileOutputStream(destinationFile)); out.write(bytes, 0, bytes.length); } finally { if (out != null) { out.close(); } } }
From source file:com.arcusys.liferay.vaadinplugin.util.ControlPanelPortletUtil.java
public static void close(OutputStream out) { if (out == null) { return;// ww w . jav a 2 s .co m } try { out.close(); } catch (IOException e) { log.warn(e); } }
From source file:com.ning.billing.beatrix.osgi.SetupBundleWithAssertion.java
private static void unTar(final File inputFile, final File outputDir) throws IOException, ArchiveException { InputStream is = null;/*from w w w . j ava2s .co m*/ TarArchiveInputStream archiveInputStream = null; TarArchiveEntry entry = null; try { is = new FileInputStream(inputFile); archiveInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is); while ((entry = (TarArchiveEntry) archiveInputStream.getNextEntry()) != null) { final File outputFile = new File(outputDir, entry.getName()); if (entry.isDirectory()) { if (!outputFile.exists()) { if (!outputFile.mkdirs()) { throw new IllegalStateException( String.format("Couldn't create directory %s.", outputFile.getAbsolutePath())); } } } else { final OutputStream outputFileStream = new FileOutputStream(outputFile); ByteStreams.copy(archiveInputStream, outputFileStream); outputFileStream.close(); } } } finally { if (archiveInputStream != null) { archiveInputStream.close(); } if (is != null) { is.close(); } } }
From source file:Main.java
public static boolean upZipFile(File zipFile, String folderPath) throws ZipException, IOException { ZipFile zfile = new ZipFile(zipFile); Enumeration<? extends ZipEntry> zList = zfile.entries(); ZipEntry ze = null;//from ww w .j a va2 s . c o m byte[] buf = new byte[1024]; while (zList.hasMoreElements()) { ze = (ZipEntry) zList.nextElement(); if (ze.isDirectory()) { continue; } Log.d(TAG, "ze.getName() = " + ze.getName()); OutputStream os = new BufferedOutputStream( new FileOutputStream(getRealFileName(folderPath, ze.getName()))); InputStream is = new BufferedInputStream(zfile.getInputStream(ze)); int readLen = 0; while ((readLen = is.read(buf, 0, 1024)) != -1) { os.write(buf, 0, readLen); } is.close(); os.close(); } zfile.close(); return true; }