KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > foundation > io > File4


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.foundation.io;
22
23 import java.io.*;
24
25 /**
26  * @sharpen.ignore
27  */

28 public class File4 {
29
30     public static void rename(String JavaDoc oldPath,String JavaDoc newPath) throws IOException {
31         if(!new java.io.File JavaDoc(oldPath).renameTo(new File(newPath))) {
32             throw new IOException("Could not rename '"+oldPath+"' to '"+newPath+"'.");
33         }
34     }
35     
36     public static void copy(String JavaDoc source, String JavaDoc target) {
37         try {
38             java.io.File JavaDoc sourceFile = new java.io.File JavaDoc(source);
39             
40             java.io.File JavaDoc targetFile = new java.io.File JavaDoc(target);
41             targetFile.mkdirs();
42             targetFile.delete();
43             
44             if (sourceFile.isDirectory()) {
45                 copyDirectory(sourceFile, targetFile);
46             } else {
47                 copyFile(sourceFile, targetFile);
48             }
49         } catch (Exception JavaDoc e) {
50             System.out.println("File.copy failed.");
51             e.printStackTrace();
52             throw new RuntimeException JavaDoc();
53         }
54     }
55
56     public static void copyFile(File source, File target) throws IOException {
57         final int bufferSize = 64000;
58
59         RandomAccessFile rafIn = new RandomAccessFile(source.getAbsolutePath(), "r");
60         RandomAccessFile rafOut = new RandomAccessFile(target.getAbsolutePath(), "rw");
61         long len = rafIn.length();
62         byte[] bytes = new byte[bufferSize];
63
64         while (len > 0) {
65             len -= bufferSize;
66             if (len < 0) {
67                 bytes = new byte[(int) (len + bufferSize)];
68             }
69             rafIn.read(bytes);
70             rafOut.write(bytes);
71         }
72
73         rafIn.close();
74         rafOut.close();
75     }
76
77     private static void copyDirectory(File source, File target) {
78         String JavaDoc[] files = source.list();
79         if (files != null) {
80             for (int i = 0; i < files.length; i++) {
81                 copy(Path4.combine(source.getAbsolutePath(), files[i]),
82                     Path4.combine(target.getAbsolutePath(), files[i]));
83             }
84         }
85     }
86     
87     public static void delete(String JavaDoc fname) {
88         new File(fname).delete();
89     }
90     
91     public static boolean exists(String JavaDoc fname){
92         return new File(fname).exists();
93     }
94
95     public static void mkdirs(String JavaDoc path) {
96         new File(path).mkdirs();
97     }
98 }
99
Popular Tags