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

1.1       bmahe       1: // IO.java
1.4     ! ylafon      2: // $Id: IO.java,v 1.3 2000/02/11 13:20:02 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.4     ! ylafon     11:  * @version $Revision: 1.3 $
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.3       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()) {
1.4     ! ylafon     79:            String filen[] = file.list();
        !            80:            for (int i = 0 ; i < filen.length ; i++) {
        !            81:                File subfile = new File(file, filen[i]);
1.3       bmahe      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