1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: FileTools.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.io
.FileInputStream
;
34 import java
.io
.FileOutputStream
;
35 import java
.io
.InputStream
;
36 import java
.io
.OutputStream
;
40 * This class deliver some functionality to copy files.
42 public class FileTools
{
45 * Copies all files under srcDir to dstDir.
46 * If dstDir does not exist, it will be created.
47 * @param srcDir the source directory
48 * @param dstDir the destination direcotry
49 * @throws java.io.IOException throws java.io.IOException if something failes
51 public static void copyDirectory(File srcDir
, File dstDir
)
52 throws java
.io
.IOException
{
53 copyDirectory(srcDir
, dstDir
, new String
[]{});
56 * Copies all files under srcDir to dstDir except Files given in the
57 * ignore list. This files will not be copied.
58 * If dstDir does not exist, it will be created.
59 * @param srcDir the source directory
60 * @param dstDir the destination direcotry
61 * @param ignore a list of files which should not be copied
62 * @throws java.io.IOException throws java.io.IOException if something failes
64 public static void copyDirectory(File srcDir
, File dstDir
, String
[] ignore
)
65 throws java
.io
.IOException
{
67 for (int i
=0; i
<ignore
.length
;i
++){
68 if (srcDir
.getName().endsWith(ignore
[i
])) {
73 if (srcDir
.isDirectory()) {
74 if (!dstDir
.exists()) {
78 String
[] files
= srcDir
.list();
79 for (int i
=0; i
< files
.length
; i
++) {
80 copyDirectory(new File(srcDir
, files
[i
]), new File(dstDir
, files
[i
]), ignore
);
83 // This method is implemented in e1071 Copying a File
84 copyFile(srcDir
, dstDir
);
89 * Copies src file to dst file. If the dst file does not exist, it is created
90 * @param src the source file
91 * @param dst the destination file
92 * @throws java.io.IOException throws java.io.IOException if something failes
94 public static void copyFile(File src
, File dst
) throws java
.io
.IOException
{
95 InputStream in
= new FileInputStream(src
);
96 OutputStream out
= new FileOutputStream(dst
);
98 // Transfer bytes from in to out
99 byte[] buf
= new byte[1024];
101 while ((len
= in
.read(buf
)) > 0) {
102 out
.write(buf
, 0, len
);
108 * Deletes all files and subdirectories under dir and the directory itself.
109 * Returns true if all deletions were successful.
110 * If the deletion fails, the method the method continues to delete rest
111 * of the files and returns false.
112 * @return Returns true if all deletions were successful, else false.
113 * @param dir the directory to delete
115 public static boolean deleteDir(File dir
) {
117 // if (! cleanDir(dir)) return false;
119 // The directory is now empty so delete it
120 // return dir.delete();
121 return cleanDir(dir
);
125 * Deletes all files and subdirectories under dir.
126 * Returns true if all deletions were successful.
127 * If a deletion fails, the method continues to delete rest of the files.
128 * @return Returns true if all deletions were successful, else false.
129 * @param dir the directory to clean from content
131 // public static boolean cleanDir(File dir){
133 // boolean success = true;
134 // if (dir.isDirectory()){
135 // File [] theFiles = dir.listFiles();
137 // if (theFiles.length != 0 )
138 // for (int i = 0; i < theFiles.length; i++){
139 // success &= theFiles[i].delete();
145 public static boolean cleanDir(File dir
)
147 if (dir
.isDirectory())
149 String
[] children
= dir
.list();
150 for (int i
=0; i
<children
.length
; i
++)
152 boolean success
= cleanDir(new File(dir
, children
[i
]));
160 // The directory is now empty so delete it