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 .
21 import java
.io
.FileInputStream
;
22 import java
.io
.FileOutputStream
;
23 import java
.io
.InputStream
;
24 import java
.io
.OutputStream
;
28 * This class deliver some functionality to copy files.
30 public class FileTools
{
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 directory
37 * @throws java.io.IOException throws java.io.IOException if something fails
39 public static void copyDirectory(File srcDir
, File dstDir
)
40 throws java
.io
.IOException
{
41 copyDirectory(srcDir
, dstDir
, new String
[]{});
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 directory
49 * @param ignore a list of files which should not be copied
50 * @throws java.io.IOException throws java.io.IOException if something fails
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
])) {
61 if (srcDir
.isDirectory()) {
62 if (!dstDir
.exists()) {
63 if (!dstDir
.mkdir()) {
64 throw new java
.io
.IOException("could not create folder " + dstDir
);
68 String
[] files
= srcDir
.list();
69 for (int i
=0; i
< files
.length
; i
++) {
70 copyDirectory(new File(srcDir
, files
[i
]), new File(dstDir
, files
[i
]), ignore
);
73 // This method is implemented in e1071 Copying a File
74 copyFile(srcDir
, dstDir
);
79 * Copies src file to dst file. If the dst file does not exist, it is created
80 * @param src the source file
81 * @param dst the destination file
82 * @throws java.io.IOException throws java.io.IOException if something fails
84 public static void copyFile(File src
, File dst
) throws java
.io
.IOException
{
85 InputStream in
= null;
86 OutputStream out
= null;
88 in
= new FileInputStream(src
);
90 out
= new FileOutputStream(dst
);
92 // Transfer bytes from in to out
93 byte[] buf
= new byte[1024];
95 while ((len
= in
.read(buf
)) > 0) {
96 out
.write(buf
, 0, len
);
109 public static boolean cleanDir(File dir
)
111 if (dir
.isDirectory())
113 String
[] children
= dir
.list();
114 for (int i
=0; i
<children
.length
; i
++)
116 boolean success
= cleanDir(new File(dir
, children
[i
]));
124 // The directory is now empty so delete it