BufferedWriter bw = null;

try {
    bw = new BufferedWriter(new FileWriter("test.txt"));
    bw.write("test");
    bw.flush(); // you can omit this if you don't care about errors while flushing
    bw.close(); // you can omit this if you don't care about errors while closing
} catch (IOException e) {
    // error handling (e.g. on flushing)
} finally {
    IOUtils.closeQuietly(bw);
}

        

public static void readURL() throws MalformedURLException, IOException {
	InputStream in = new URL("http://commons.apache.org").openStream();
	try {
		System.out.println(IOUtils.toString(in));
	} finally {
		IOUtils.closeQuietly(in);
	}
}

        

try {
    // write to bw.
    bw.close(); // throw IOException if an error occurs.

} finally {
    // don't clobber a previous IOException
    IOUtils.closeQuietly(bw);
}

        

InputStream in = new URL( "http://commons.apache.org" ).openStream();
try {
	System.out.println( IOUtils.toString( in ) );
} finally {
	IOUtils.closeQuietly(in);
}

        

public void whenConvertingInProgressToFile_thenCorrect() 
  throws IOException {
  
    InputStream initialStream = new FileInputStream(
      new File("src/main/resources/sample.txt"));
    File targetFile = new File("src/main/resources/targetFile.tmp");
    OutputStream outStream = new FileOutputStream(targetFile);
 
    byte[] buffer = new byte[8 * 1024];
    int bytesRead;
    while ((bytesRead = initialStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, bytesRead);
    }
    IOUtils.closeQuietly(initialStream);
    IOUtils.closeQuietly(outStream);
}

        

public static void docxToHtml(String docxFilePath, String htmlPath) throws Exception {
	OutputStream output = null;
	try {
		//
		WordprocessingMLPackage wmlPackage = WordprocessingMLPackage.load(new File(docxFilePath));

		WMLPACKAGE_BUILDER.configChineseFonts(wmlPackage).configSimSunFont(wmlPackage);
		
		WMLPACKAGE_WRITER.writeToHtml(wmlPackage, htmlPath);

	} catch (Exception ex) {
		ex.printStackTrace();
	} finally {
		IOUtils.closeQuietly(output);
	}

}

        

private void sendFavicon(HttpServletResponse response) throws IOException
{
	InputStream fis = null;
	OutputStream out = null;
	try
	{
		response.setContentType("image/x-icon");
		fis = WebServlet.class.getResourceAsStream("/resources/icon.ico");
		out = response.getOutputStream();
		IOUtils.copy(fis, out);
	}
	finally
	{
		IOUtils.closeQuietly(out);
		IOUtils.closeQuietly(fis);
	}
	return;
}

        

protected void copyStreamToJar(InputStream zin, ZipOutputStream out, String currentName, long fileTime) throws IOException {
    // Create new entry for zip file.

    ZipEntry newEntry = new ZipEntry(currentName);
    // Make sure there is date and time set.
    if (fileTime != -1) {
        newEntry.setTime(fileTime); // If found set it into output file.
    }
    out.putNextEntry(newEntry);
    if (zin != null) {
        IOUtils.copy(zin, out);
    }
    IOUtils.closeQuietly(zin);

}

        

public static ProcessDefinition parseProcess(byte[] bytes,long processId,boolean parseChildren) throws Exception{
	ByteArrayInputStream bin=new ByteArrayInputStream(bytes);
	try{
		SAXReader reader=new SAXReader();
		Document document=reader.read(bin);
		Element root=document.getRootElement();
		if(processParser.support(root)){
			ProcessDefinition pd=(ProcessDefinition)processParser.parse(root,processId,parseChildren);
			return pd;
		}
		return null;
	}finally{
		IOUtils.closeQuietly(bin);
	}
}

        

public synchronized void releaseBuffer(ByteBuffer buffer) {
  if (buffer == EMPTY_BUFFER) return;
  Object val = getExtendedReadBuffers().remove(buffer);
  if (val == null) {
    throw new IllegalArgumentException("tried to release a buffer " +
        "that was not created by this stream, " + buffer);
  }
  if (val instanceof ClientMmap) {
    IOUtils.closeQuietly((ClientMmap)val);
  } else if (val instanceof ByteBufferPool) {
    ((ByteBufferPool)val).putBuffer(buffer);
  }
}        
main