Annotation of java/classes/org/w3c/util/IO.java, revision 1.2.4.1

1.1       bmahe       1: // IO.java
1.2.4.1 ! bmahe       2: // $Id: IO.java,v 1.2 1999/09/02 14:04:17 bmahe Exp $
1.1       bmahe       3: // (c) COPYRIGHT MIT and INRIA, 1998.
                      4: // Please first read the full copyright statement in file COPYRIGHT.html
                      5:  
                      6: package org.w3c.util;
                      7: 
                      8: import java.io.*;
                      9: 
                     10: /**
1.2.4.1 ! bmahe      11:  * @version $Revision: 1.2 $
1.1       bmahe      12:  * @author  BenoοΏ½t MahοΏ½ (bmahe@w3.org)
                     13:  */
                     14: public class IO {
                     15: 
                     16:     /**
                     17:      * Copy source into dest.
                     18:      */
                     19:     public static void copy(File source, File dest) 
                     20:        throws IOException
                     21:     {
                     22:        BufferedInputStream in = 
                     23:            new BufferedInputStream( new FileInputStream(source) );
                     24:        BufferedOutputStream out =
                     25:            new BufferedOutputStream( new FileOutputStream(dest) );
                     26:        byte buffer[] = new byte[1024];
                     27:        int read = -1;
                     28:        while ((read = in.read(buffer, 0, 1024)) != -1)
                     29:            out.write(buffer, 0, read);
                     30:        out.flush();
                     31:        out.close();
                     32:        in.close();
                     33:     }
1.2       bmahe      34: 
                     35:     /**
                     36:      * Copy source into dest.
                     37:      */
                     38:     public static void copy(InputStream in, OutputStream out) 
                     39:        throws IOException
                     40:     {
                     41:        BufferedInputStream bin = 
                     42:            new BufferedInputStream(in);
                     43:        BufferedOutputStream bout =
                     44:            new BufferedOutputStream(out);
                     45:        byte buffer[] = new byte[1024];
                     46:        int read = -1;
                     47:        while ((read = bin.read(buffer, 0, 1024)) != -1)
                     48:            bout.write(buffer, 0, read);
                     49:        bout.flush();
                     50:        bout.close();
                     51:        bin.close();
                     52:     } 
1.2.4.1 ! bmahe      53: 
        !            54:     /**
        !            55:      * Delete recursivly
        !            56:      * @param file the file (or directory) to delete.
        !            57:      */
        !            58:     public static boolean delete(File file) {
        !            59:        if (file.exists()) {
        !            60:            if (file.isDirectory()) {
        !            61:                if (clean(file)) {
        !            62:                    return file.delete();
        !            63:                } else {
        !            64:                    return false;
        !            65:                }
        !            66:            } else {
        !            67:                return file.delete();
        !            68:            }
        !            69:        }
        !            70:        return true;
        !            71:     }
        !            72: 
        !            73:     /**
        !            74:      * Clean recursivly
        !            75:      * @param file the directory to clean
        !            76:      */
        !            77:     public static boolean clean(File file) {
        !            78:        if (file.isDirectory()) {
        !            79:            File files[] = file.listFiles();
        !            80:            for (int i = 0 ; i < files.length ; i++) {
        !            81:                File subfile = files[i];
        !            82:                if ((subfile.isDirectory()) && (! clean(subfile))) {
        !            83:                    return false;
        !            84:                } else if (! subfile.delete()) {
        !            85:                    return false;
        !            86:                }
        !            87:            }
        !            88:        }
        !            89:        return true;
        !            90:     }
        !            91: 
1.1       bmahe      92: }

Webmaster