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.releaser.util.file;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 import fr.paris.lutece.portal.service.util.AppLogService;
42
43
44
45
46
47 public class FileUtils
48 {
49
50
51 public static final boolean STATUS_OK = true;
52
53
54 public static final boolean STATUS_ERROR = false;
55
56
57
58
59
60
61
62
63
64
65 public static boolean delete( File file, StringBuffer logBuffer )
66 {
67 if ( file.isDirectory( ) )
68 {
69 boolean bStatus = STATUS_OK;
70
71 for ( String fileName : file.list( ) )
72 {
73
74 bStatus &= delete( new File( file.getAbsolutePath( ) + File.separator + fileName ), logBuffer );
75 }
76 }
77
78 logBuffer.append( "DELETING " + file.getAbsolutePath( ) + "\n" );
79
80 if ( !file.delete( ) )
81 {
82 logBuffer.append( "UNABLE TO DELETE : " + file.getAbsolutePath( ) );
83
84 return STATUS_ERROR;
85 }
86
87 return STATUS_OK;
88 }
89
90
91
92
93
94
95
96
97 public static String readFile( String strFilePath )
98 {
99 File file = new File( strFilePath );
100 String strFile = null;
101 try
102 {
103 strFile = org.apache.commons.io.FileUtils.readFileToString( file, "UTF-8" );
104 }
105 catch( IOException e )
106 {
107
108 AppLogService.error( e );
109 }
110 return strFile;
111
112 }
113
114
115
116
117
118
119
120
121
122
123 public static List<String> list( String strDirPath, String strFileExtension )
124 {
125 return list( strDirPath, strFileExtension, true );
126 }
127
128
129
130
131
132
133
134
135
136
137
138
139 public static List<String> list( String strDirPath, String strFileExtension, boolean bRecursive )
140 {
141 List<String> strFileList = new ArrayList<String>( );
142 File file = new File( strDirPath );
143
144 for ( File fileChild : file.listFiles( ) )
145 {
146 if ( fileChild.isDirectory( ) && bRecursive )
147 {
148 strFileList.addAll( list( fileChild.getAbsolutePath( ), strFileExtension, bRecursive ) );
149 }
150 else
151 if ( fileChild.isFile( ) && ( ( strFileExtension == null ) || file.getName( ).endsWith( strFileExtension ) ) )
152 {
153 strFileList.add( file.getName( ) );
154 }
155 }
156
157 return strFileList;
158 }
159
160 }