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 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
[]{});
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
])) {
61 if (srcDir
.isDirectory()) {
62 if (!dstDir
.exists()) {
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
);
71 // This method is implemented in e1071 Copying a File
72 copyFile(srcDir
, dstDir
);
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;
86 in
= new FileInputStream(src
);
88 out
= new FileOutputStream(dst
);
90 // Transfer bytes from in to out
91 byte[] buf
= new byte[1024];
93 while ((len
= in
.read(buf
)) > 0) {
94 out
.write(buf
, 0, len
);
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
]));
122 // The directory is now empty so delete it