KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Ostermiller > util > FileHelper


1 /*
2  * Static File routines.
3  * Copyright (C) 2002 Stephen Ostermiller
4  * http://ostermiller.org/contact.pl?regarding=Java+Utilities
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * See COPYING.TXT for details.
17  */

18
19 package com.Ostermiller.util;
20
21 import java.io.*;
22 import java.text.MessageFormat JavaDoc;
23 import java.util.ResourceBundle JavaDoc;
24 import java.util.Locale JavaDoc;
25
26 /**
27  * Utilities for File manipulation.
28  * More information about this class is available from <a target="_top" HREF=
29  * "http://ostermiller.org/utils/FileHelper.html">ostermiller.org</a>.
30  *
31  * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
32  * @since ostermillerutils 1.00.00
33  */

34 public class FileHelper {
35
36     /**
37      * Locale specific strings displayed to the user.
38      *
39      * @since ostermillerutils 1.00.00
40      */

41     protected static ResourceBundle JavaDoc labels = ResourceBundle.getBundle("com.Ostermiller.util.FileHelper", Locale.getDefault());
42
43
44     /**
45      * Move a file from one location to another. An attempt is made to rename
46      * the file and if that fails, the file is copied and the old file deleted.
47      *
48      * If the destination file already exists, an exception will be thrown.
49      *
50      * @param from file which should be moved.
51      * @param to desired destination of the file.
52      * @throws IOException if an error occurs.
53      *
54      * @since ostermillerutils 1.00.00
55      */

56     public static void move(File from, File to) throws IOException {
57         move(from, to, false);
58     }
59
60     /**
61      * Move a file from one location to another. An attempt is made to rename
62      * the file and if that fails, the file is copied and the old file deleted.
63      *
64      * @param from file which should be moved.
65      * @param to desired destination of the file.
66      * @param overwrite If false, an exception will be thrown rather than overwrite a file.
67      * @throws IOException if an error occurs.
68      *
69      * @since ostermillerutils 1.00.00
70      */

71     public static void move(File from, File to, boolean overwrite) throws IOException {
72         if (to.exists()){
73             if (overwrite){
74                 if (!to.delete()){
75                     throw new IOException(
76                         MessageFormat.format(
77                             labels.getString("deleteerror"),
78                             (Object JavaDoc[])new String JavaDoc[] {
79                                 to.toString()
80                             }
81                         )
82                     );
83                 }
84             } else {
85                 throw new IOException(
86                     MessageFormat.format(
87                         labels.getString("alreadyexistserror"),
88                         (Object JavaDoc[])new String JavaDoc[] {
89                             to.toString()
90                         }
91                     )
92                 );
93             }
94         }
95
96         if (from.renameTo(to)) return;
97
98         InputStream in = null;
99         OutputStream out = null;
100         try {
101             in = new FileInputStream(from);
102             out = new FileOutputStream(to);
103             copy(in, out);
104             in.close();
105             in = null;
106             out.flush();
107             out.close();
108             out = null;
109             if (!from.delete()){
110                 throw new IOException(
111                     MessageFormat.format(
112                         labels.getString("deleteoriginalerror"),
113                         (Object JavaDoc[])new String JavaDoc[] {
114                             from.toString(),
115                             to.toString()
116                         }
117                     )
118                 );
119             }
120         } finally {
121             if (in != null){
122                 in.close();
123                 in = null;
124             }
125             if (out != null){
126                 out.flush();
127                 out.close();
128                 out = null;
129             }
130         }
131     }
132
133     /**
134      * Buffer size when reading from input stream.
135      *
136      * @since ostermillerutils 1.00.00
137      */

138     private final static int BUFFER_SIZE = 1024;
139
140     /**
141      * Copy the data from the input stream to the output stream.
142      *
143      * @param in data source
144      * @param out data destination
145      * @throws IOException in an input or output error occurs
146      *
147      * @since ostermillerutils 1.00.00
148      */

149     private static void copy(InputStream in, OutputStream out) throws IOException {
150         byte[] buffer = new byte[BUFFER_SIZE];
151         int read;
152         while((read = in.read(buffer)) != -1){
153             out.write(buffer, 0, read);
154         }
155     }
156 }
157
Popular Tags