update dev300-m58
[ooovba.git] / qadevOOo / runner / helper / FileTools.java
blob7295e2ca6635d9d913c603d557d2f5a4c6b7a592
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FileTools.java,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package helper;
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 import java.io.InputStream;
36 import java.io.OutputStream;
39 /**
40 * This class deliver some functionality to copy files.
42 public class FileTools {
44 /**
45 * Copies all files under srcDir to dstDir.
46 * If dstDir does not exist, it will be created.
47 * @param srcDir the source directory
48 * @param dstDir the destination direcotry
49 * @throws java.io.IOException throws java.io.IOException if something failes
50 */
51 public static void copyDirectory(File srcDir, File dstDir)
52 throws java.io.IOException {
53 copyDirectory(srcDir, dstDir, new String[]{});
55 /**
56 * Copies all files under srcDir to dstDir except Files given in the
57 * ignore list. This files will not be copied.
58 * If dstDir does not exist, it will be created.
59 * @param srcDir the source directory
60 * @param dstDir the destination direcotry
61 * @param ignore a list of files which should not be copied
62 * @throws java.io.IOException throws java.io.IOException if something failes
63 */
64 public static void copyDirectory(File srcDir, File dstDir, String[] ignore)
65 throws java.io.IOException {
67 for (int i=0; i<ignore.length;i++){
68 if (srcDir.getName().endsWith(ignore[i])) {
69 return;
73 if (srcDir.isDirectory()) {
74 if (!dstDir.exists()) {
75 dstDir.mkdir();
78 String[] files = srcDir.list();
79 for (int i=0; i< files.length; i++) {
80 copyDirectory(new File(srcDir, files[i]), new File(dstDir, files[i]), ignore);
82 } else {
83 // This method is implemented in e1071 Copying a File
84 copyFile(srcDir, dstDir);
88 /**
89 * Copies src file to dst file. If the dst file does not exist, it is created
90 * @param src the source file
91 * @param dst the destination file
92 * @throws java.io.IOException throws java.io.IOException if something failes
93 */
94 public static void copyFile(File src, File dst) throws java.io.IOException {
95 InputStream in = new FileInputStream(src);
96 OutputStream out = new FileOutputStream(dst);
98 // Transfer bytes from in to out
99 byte[] buf = new byte[1024];
100 int len;
101 while ((len = in.read(buf)) > 0) {
102 out.write(buf, 0, len);
104 in.close();
105 out.close();
108 * Deletes all files and subdirectories under dir and the directory itself.
109 * Returns true if all deletions were successful.
110 * If the deletion fails, the method the method continues to delete rest
111 * of the files and returns false.
112 * @return Returns true if all deletions were successful, else false.
113 * @param dir the directory to delete
115 public static boolean deleteDir(File dir) {
117 // if (! cleanDir(dir)) return false;
119 // The directory is now empty so delete it
120 // return dir.delete();
121 return cleanDir(dir);
125 * Deletes all files and subdirectories under dir.
126 * Returns true if all deletions were successful.
127 * If a deletion fails, the method continues to delete rest of the files.
128 * @return Returns true if all deletions were successful, else false.
129 * @param dir the directory to clean from content
131 // public static boolean cleanDir(File dir){
133 // boolean success = true;
134 // if (dir.isDirectory()){
135 // File [] theFiles = dir.listFiles();
137 // if (theFiles.length != 0 )
138 // for (int i = 0; i < theFiles.length; i++){
139 // success &= theFiles[i].delete();
140 // }
141 // }
142 // return success;
143 // }
145 public static boolean cleanDir(File dir)
147 if (dir.isDirectory())
149 String[] children = dir.list();
150 for (int i=0; i<children.length; i++)
152 boolean success = cleanDir(new File(dir, children[i]));
153 if (!success)
155 return false;
160 // The directory is now empty so delete it
161 return dir.delete();