1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.xmlpage.util;
35
36 import fr.paris.lutece.portal.service.util.AppLogService;
37
38 import java.io.File;
39 import java.io.FileInputStream;
40 import java.io.FileOutputStream;
41 import java.io.IOException;
42
43 import java.nio.channels.FileChannel;
44
45
46
47
48
49 public class XmlPageFileUtils
50 {
51
52
53
54 protected XmlPageFileUtils( )
55 {
56
57 }
58
59
60
61
62
63
64 public static boolean makeDirectory( String strDirectoryPath )
65 {
66 File fDirectory = new File( strDirectoryPath );
67 AppLogService.debug( "Creating directory : " + strDirectoryPath );
68
69 return fDirectory.mkdirs( );
70 }
71
72
73
74
75
76
77 public static boolean createFile( String strFilePath )
78 {
79 AppLogService.debug( "Creating file : " + strFilePath );
80
81 File fFile = new File( strFilePath );
82 boolean bReturn = false;
83
84 try
85 {
86 bReturn = fFile.createNewFile( );
87 }
88 catch ( IOException ioe )
89 {
90 AppLogService.error( ioe.getMessage( ), ioe );
91 }
92
93 return bReturn;
94 }
95
96
97
98
99
100
101
102 public static void copyFile( String strDirectory, String strFileName, String strNewDirectory )
103 {
104 try
105 {
106 AppLogService.debug( "Copying file \"" + strFileName + "\" from directory \"" + strDirectory +
107 "\" to directory \"" + strNewDirectory + "\"" );
108
109 FileInputStream fInputStream = new FileInputStream( strDirectory.concat( "/" ).concat( strFileName ) );
110 FileOutputStream fOutputStream = new FileOutputStream( strNewDirectory.concat( "/" ).concat( strFileName ) );
111 FileChannel fInputChannel = fInputStream.getChannel( );
112 FileChannel fOutputChannel = fOutputStream.getChannel( );
113 fInputChannel.transferTo( 0, fInputChannel.size( ), fOutputChannel );
114
115 fInputChannel.close( );
116 fOutputChannel.close( );
117
118 fInputStream.close( );
119 fOutputStream.close( );
120 AppLogService.debug( "Copying file done" );
121 }
122 catch ( IOException ioe )
123 {
124 AppLogService.error( ioe.getMessage( ), ioe );
125 }
126 }
127
128
129
130
131
132
133
134 public static boolean renameDirectory( String strOriginalDirectory, String strNewDirectory )
135 {
136 AppLogService.debug( "Renaming directory from \"" + strOriginalDirectory + "\" to \"" + strNewDirectory + "\"" );
137
138 File fOriginal = new File( strOriginalDirectory );
139 File fNew = new File( strNewDirectory );
140
141 return fOriginal.renameTo( fNew );
142 }
143
144
145
146
147
148
149 public static boolean removeFile( String strFilePath )
150 {
151 AppLogService.debug( "Removing file \"" + strFilePath + "\"" );
152
153 return removeDirectory( new File( strFilePath ) );
154 }
155
156
157
158
159
160
161 public static boolean removeDirectory( String strDirectoryPath )
162 {
163 AppLogService.debug( "Removing directory \"" + strDirectoryPath + "\"" );
164
165 return removeDirectory( new File( strDirectoryPath ) );
166 }
167
168
169
170
171
172
173 public static boolean removeDirectory( File fDirectoryPath )
174 {
175 if ( fDirectoryPath.isDirectory( ) )
176 {
177 File[] childs = fDirectoryPath.listFiles( );
178
179 for ( int i = 0; i < childs.length; i++ )
180 {
181 if ( !removeDirectory( childs[i] ) )
182 {
183 return false;
184 }
185 }
186 }
187
188 return fDirectoryPath.delete( );
189 }
190 }