bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / runner / helper / FileTools.java
blob4bcee70a52ce3e78f556af88592789d618810272
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 = null;
84 OutputStream out = null;
85 try {
86 in = new FileInputStream(src);
87 try {
88 out = new FileOutputStream(dst);
90 // Transfer bytes from in to out
91 byte[] buf = new byte[1024];
92 int len;
93 while ((len = in.read(buf)) > 0) {
94 out.write(buf, 0, len);
96 } finally {
97 if (out != null)
98 out.close();
100 } finally {
101 if (in != null)
102 in.close();
107 public static boolean cleanDir(File dir)
109 if (dir.isDirectory())
111 String[] children = dir.list();
112 for (int i=0; i<children.length; i++)
114 boolean success = cleanDir(new File(dir, children[i]));
115 if (!success)
117 return false;
122 // The directory is now empty so delete it
123 return dir.delete();