bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / runner / helper / FileTools.java
blob760da29c07f8f6746603c15d86e60a521ff91508
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package helper;
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.FileOutputStream;
23 import java.io.InputStream;
24 import java.io.OutputStream;
27 /**
28 * This class deliver some functionality to copy files.
30 public class FileTools {
32 /**
33 * Copies all files under srcDir to dstDir.
34 * If dstDir does not exist, it will be created.
35 * @param srcDir the source directory
36 * @param dstDir the destination direcotry
37 * @throws java.io.IOException throws java.io.IOException if something failes
39 public static void copyDirectory(File srcDir, File dstDir)
40 throws java.io.IOException {
41 copyDirectory(srcDir, dstDir, new String[]{});
43 /**
44 * Copies all files under srcDir to dstDir except Files given in the
45 * ignore list. This files will not be copied.
46 * If dstDir does not exist, it will be created.
47 * @param srcDir the source directory
48 * @param dstDir the destination direcotry
49 * @param ignore a list of files which should not be copied
50 * @throws java.io.IOException throws java.io.IOException if something failes
52 public static void copyDirectory(File srcDir, File dstDir, String[] ignore)
53 throws java.io.IOException {
55 for (int i=0; i<ignore.length;i++){
56 if (srcDir.getName().endsWith(ignore[i])) {
57 return;
61 if (srcDir.isDirectory()) {
62 if (!dstDir.exists()) {
63 dstDir.mkdir();
66 String[] files = srcDir.list();
67 for (int i=0; i< files.length; i++) {
68 copyDirectory(new File(srcDir, files[i]), new File(dstDir, files[i]), ignore);
70 } else {
71 // This method is implemented in e1071 Copying a File
72 copyFile(srcDir, dstDir);
76 /**
77 * Copies src file to dst file. If the dst file does not exist, it is created
78 * @param src the source file
79 * @param dst the destination file
80 * @throws java.io.IOException throws java.io.IOException if something failes
82 public static void copyFile(File src, File dst) throws java.io.IOException {
83 InputStream in = new FileInputStream(src);
84 OutputStream out = new FileOutputStream(dst);
86 // Transfer bytes from in to out
87 byte[] buf = new byte[1024];
88 int len;
89 while ((len = in.read(buf)) > 0) {
90 out.write(buf, 0, len);
92 in.close();
93 out.close();
95 /**
96 * Deletes all files and subdirectories under dir and the directory itself.
97 * Returns true if all deletions were successful.
98 * If the deletion fails, the method the method continues to delete rest
99 * of the files and returns false.
100 * @return Returns true if all deletions were successful, else false.
101 * @param dir the directory to delete
103 public static boolean deleteDir(File dir) {
105 // if (! cleanDir(dir)) return false;
107 // The directory is now empty so delete it
108 // return dir.delete();
109 return cleanDir(dir);
113 * Deletes all files and subdirectories under dir.
114 * Returns true if all deletions were successful.
115 * If a deletion fails, the method continues to delete rest of the files.
116 * @return Returns true if all deletions were successful, else false.
117 * @param dir the directory to clean from content
119 // public static boolean cleanDir(File dir){
121 // boolean success = true;
122 // if (dir.isDirectory()){
123 // File [] theFiles = dir.listFiles();
125 // if (theFiles.length != 0 )
126 // for (int i = 0; i < theFiles.length; i++){
127 // success &= theFiles[i].delete();
128 // }
129 // }
130 // return success;
131 // }
133 public static boolean cleanDir(File dir)
135 if (dir.isDirectory())
137 String[] children = dir.list();
138 for (int i=0; i<children.length; i++)
140 boolean success = cleanDir(new File(dir, children[i]));
141 if (!success)
143 return false;
148 // The directory is now empty so delete it
149 return dir.delete();