Source code: com/pepperview/util/Util.java
1 /*
2 * Util.java -
3 * Copyright (C) 2000 Fabrice Armisen
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 package com.pepperview.util;
21
22 import java.io.IOException;
23 import java.io.File;
24 import java.io.BufferedWriter;
25 import java.io.BufferedReader;
26 import java.io.FileWriter;
27 import java.io.FileReader;
28 import java.io.FileInputStream;
29 import java.io.FileOutputStream;
30
31 /**
32 * Utilities
33 *
34 * @author Fabrice Armisen
35 * @created May 15, 2001
36 */
37 public class Util
38 {
39
40 /**
41 * Copy a file
42 *
43 * @param source the source file
44 * @param target the destination file
45 * @exception IOException
46 * @since
47 */
48 public static void copyFile( File source, File target ) throws IOException
49 {
50 FileOutputStream out = null;
51 FileInputStream in = null;
52
53 try
54 {
55 in = new FileInputStream( source );
56 out = new FileOutputStream( target );
57 byte[] buf = new byte[1024];
58 int i = 0;
59 while ( ( i = in.read( buf ) ) != -1 )
60 {
61 out.write( buf, 0, i );
62 }
63 }
64 finally
65 {
66 if ( null != in )
67 {
68 in.close();
69 }
70
71 if ( null != out )
72 {
73 out.close();
74 }
75 }
76
77 /*
78 * BufferedWriter out = null;
79 * BufferedReader in = null;
80 * try
81 * {
82 * out = new BufferedWriter( new FileWriter( target ) );
83 * in = new BufferedReader( new FileReader( source ) );
84 * int ch;
85 * ch = in.read();
86 * while ( ch != -1 )
87 * {
88 * out.write( ch );
89 * ch = in.read();
90 * }
91 * }
92 * finally
93 * {
94 * if ( null != in )
95 * {
96 * in.close();
97 * }
98 * if ( null != out )
99 * {
100 * out.close();
101 * }
102 * }
103 */
104 }
105
106
107
108 /**
109 * Move a file
110 *
111 * @param source the source file
112 * @param target the destination file
113 * @exception IOException
114 * @since
115 */
116 public static void moveFile( File source, File target ) throws IOException
117 {
118 if ( target.isDirectory() )
119 {
120 copyFile( source, new File( target, source.getName() ) );
121 }
122 else
123 {
124 copyFile( source, target );
125 }
126 source.delete();
127 }
128 }
129