Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / runner / helper / FileTools.java
blob0a6b4dd965777dc58924ef381c96a487b62dadb7
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 package helper;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.InputStream;
33 import java.io.OutputStream;
36 /**
37 * This class deliver some functionality to copy files.
39 public class FileTools {
41 /**
42 * Copies all files under srcDir to dstDir.
43 * If dstDir does not exist, it will be created.
44 * @param srcDir the source directory
45 * @param dstDir the destination direcotry
46 * @throws java.io.IOException throws java.io.IOException if something failes
48 public static void copyDirectory(File srcDir, File dstDir)
49 throws java.io.IOException {
50 copyDirectory(srcDir, dstDir, new String[]{});
52 /**
53 * Copies all files under srcDir to dstDir except Files given in the
54 * ignore list. This files will not be copied.
55 * If dstDir does not exist, it will be created.
56 * @param srcDir the source directory
57 * @param dstDir the destination direcotry
58 * @param ignore a list of files which should not be copied
59 * @throws java.io.IOException throws java.io.IOException if something failes
61 public static void copyDirectory(File srcDir, File dstDir, String[] ignore)
62 throws java.io.IOException {
64 for (int i=0; i<ignore.length;i++){
65 if (srcDir.getName().endsWith(ignore[i])) {
66 return;
70 if (srcDir.isDirectory()) {
71 if (!dstDir.exists()) {
72 dstDir.mkdir();
75 String[] files = srcDir.list();
76 for (int i=0; i< files.length; i++) {
77 copyDirectory(new File(srcDir, files[i]), new File(dstDir, files[i]), ignore);
79 } else {
80 // This method is implemented in e1071 Copying a File
81 copyFile(srcDir, dstDir);
85 /**
86 * Copies src file to dst file. If the dst file does not exist, it is created
87 * @param src the source file
88 * @param dst the destination file
89 * @throws java.io.IOException throws java.io.IOException if something failes
91 public static void copyFile(File src, File dst) throws java.io.IOException {
92 InputStream in = new FileInputStream(src);
93 OutputStream out = new FileOutputStream(dst);
95 // Transfer bytes from in to out
96 byte[] buf = new byte[1024];
97 int len;
98 while ((len = in.read(buf)) > 0) {
99 out.write(buf, 0, len);
101 in.close();
102 out.close();
105 * Deletes all files and subdirectories under dir and the directory itself.
106 * Returns true if all deletions were successful.
107 * If the deletion fails, the method the method continues to delete rest
108 * of the files and returns false.
109 * @return Returns true if all deletions were successful, else false.
110 * @param dir the directory to delete
112 public static boolean deleteDir(File dir) {
114 // if (! cleanDir(dir)) return false;
116 // The directory is now empty so delete it
117 // return dir.delete();
118 return cleanDir(dir);
122 * Deletes all files and subdirectories under dir.
123 * Returns true if all deletions were successful.
124 * If a deletion fails, the method continues to delete rest of the files.
125 * @return Returns true if all deletions were successful, else false.
126 * @param dir the directory to clean from content
128 // public static boolean cleanDir(File dir){
130 // boolean success = true;
131 // if (dir.isDirectory()){
132 // File [] theFiles = dir.listFiles();
134 // if (theFiles.length != 0 )
135 // for (int i = 0; i < theFiles.length; i++){
136 // success &= theFiles[i].delete();
137 // }
138 // }
139 // return success;
140 // }
142 public static boolean cleanDir(File dir)
144 if (dir.isDirectory())
146 String[] children = dir.list();
147 for (int i=0; i<children.length; i++)
149 boolean success = cleanDir(new File(dir, children[i]));
150 if (!success)
152 return false;
157 // The directory is now empty so delete it
158 return dir.delete();