KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > savant > FileTools


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
3  */

4 package com.inversoft.savant;
5
6
7 import java.io.BufferedInputStream JavaDoc;
8 import java.io.BufferedOutputStream JavaDoc;
9 import java.io.File JavaDoc;
10 import java.io.FileOutputStream JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.InputStream JavaDoc;
13
14
15 /**
16  * <p>
17  * This class stores common helper methods for dealing with
18  * files.
19  * </p>
20  *
21  * @author Brian Pontarelli
22  */

23 public class FileTools {
24
25     /**
26      * Reads from the given input stream and writes the contents out to the given
27      * file.
28      *
29      * @param is The InputStream to read from. This InputStream is wrapped in
30      * a BufferedInputStream for performance.
31      * @param output The file to output to.
32      * @throws IOException If the output operation fails.
33      */

34     public static void output(InputStream JavaDoc is, File JavaDoc output)
35     throws IOException JavaDoc {
36         BufferedInputStream JavaDoc bis = null;
37         BufferedOutputStream JavaDoc bos = null;
38         try {
39             bis = new BufferedInputStream JavaDoc(is);
40             bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(output));
41             byte[] b = new byte[1024];
42             int len;
43             while ((len = bis.read(b)) != -1) {
44                 bos.write(b, 0, len);
45             }
46         } finally {
47             if (bis != null) {
48                 bis.close();
49             }
50
51             if (bos != null) {
52                 bos.close();
53             }
54         }
55     }
56 }
57
Popular Tags