??1
/**/
/*
??2
?*?Made?In?GamVan.com
??3
?*?Created?on?2005年3月18日,?下午8:37
??4
?
*/
??5
package
?com.gamvan.tools;
??6
import
?java.io.BufferedReader;
??7
import
?java.io.File;
??8
import
?java.io.FileInputStream;
??9
import
?java.io.FileOutputStream;
?10
import
?java.io.FileWriter;
?11
import
?java.io.IOException;
?12
import
?java.io.InputStream;
?13
import
?java.io.InputStreamReader;
?14
import
?java.io.PrintWriter;
?15
import
?java.util.StringTokenizer;
?16
public
?
class
?FileOperate?
{
?17
????
private
?String?message;
?18
????
public
?FileOperate()?
{
?19
????}
?20
?21
????
/**?*/
/**
?22
?????*?讀取文本文件內容
?23
?????*?
@param
?filePathAndName?帶有完整絕對路徑的文件名
?24
?????*?
@param
?encoding?文本文件打開的編碼方式
?25
?????*?
@return
?返回文本文件的內容
?26
?????
*/
?27
????
public
?String?readTxt(String?filePathAndName,String?encoding)?
throws
?IOException
{
?28
?????encoding?
=
?encoding.trim();
?29
?????StringBuffer?str?
=
?
new
?StringBuffer(
""
);
?30
?????String?st?
=
?
""
;
?31
?????
try
{
?32
??????FileInputStream?fs?
=
?
new
?FileInputStream(filePathAndName);
?33
??????InputStreamReader?isr;
?34
??????
if
(encoding.equals(
""
))
{
?35
???????isr?
=
?
new
?InputStreamReader(fs);
?36
??????}
else
{
?37
???????isr?
=
?
new
?InputStreamReader(fs,encoding);
?38
??????}
?39
??????BufferedReader?br?
=
?
new
?BufferedReader(isr);
?40
??????
try
{
?41
???????String?data?
=
?
""
;
?42
???????
while
((data?
=
?br.readLine())
!=
null
)
{
?43
?????????str.append(data
+
"
?
"
);?
?44
???????}
?45
??????}
catch
(Exception?e)
{
?46
???????str.append(e.toString());
?47
??????}
?48
??????st?
=
?str.toString();
?49
?????}
catch
(IOException?es)
{
?50
??????st?
=
?
""
;
?51
?????}
?52
?????
return
?st;?????
?53
????}
?54
?55
????
/**?*/
/**
?56
?????*?新建目錄
?57
?????*?
@param
?folderPath?目錄
?58
?????*?
@return
?返回目錄創建后的路徑
?59
?????
*/
?60
????
public
?String?createFolder(String?folderPath)?
{
?61
????????String?txt?
=
?folderPath;
?62
????????
try
?
{
?63
????????????java.io.File?myFilePath?
=
?
new
?java.io.File(txt);
?64
????????????txt?
=
?folderPath;
?65
????????????
if
?(
!
myFilePath.exists())?
{
?66
????????????????myFilePath.mkdir();
?67
????????????}
?68
????????}
?69
????????
catch
?(Exception?e)?
{
?70
????????????message?
=
?
"
創建目錄操作出錯
"
;
?71
????????}
?72
????????
return
?txt;
?73
????}
?74
????
?75
????
/**?*/
/**
?76
?????*?多級目錄創建
?77
?????*?
@param
?folderPath?準備要在本級目錄下創建新目錄的目錄路徑?例如?c:myf
?78
?????*?
@param
?paths?無限級目錄參數,各級目錄以單數線區分?例如?a|b|c
?79
?????*?
@return
?返回創建文件后的路徑?例如?c:myfac
?80
?????
*/
?81
????
public
?String?createFolders(String?folderPath,?String?paths)
{
?82
????????String?txts?
=
?folderPath;
?83
????????
try
{
?84
????????????String?txt;
?85
????????????txts?
=
?folderPath;
?86
????????????StringTokenizer?st?
=
?
new
?StringTokenizer(paths,
"
|
"
);
?87
????????????
for
(
int
?i
=
0
;?st.hasMoreTokens();?i
++
)
{
?88
????????????????????txt?
=
?st.nextToken().trim();
?89
????????????????????
if
(txts.lastIndexOf(
"
/
"
)
!=-
1
)
{?
?90
????????????????????????txts?
=
?createFolder(txts
+
txt);
?91
????????????????????}
else
{
?92
????????????????????????txts?
=
?createFolder(txts
+
txt
+
"
/
"
);????
?93
????????????????????}
?94
????????????}
?95
???????}
catch
(Exception?e)
{
?96
???????????message?
=
?
"
創建目錄操作出錯!
"
;
?97
???????}
?98
????????
return
?txts;
?99
????}
100
101
????
102
????
/**?*/
/**
103
?????*?新建文件
104
?????*?
@param
?filePathAndName?文本文件完整絕對路徑及文件名
105
?????*?
@param
?fileContent?文本文件內容
106
?????*?
@return
107
?????
*/
108
????
public
?
void
?createFile(String?filePathAndName,?String?fileContent)?
{
109
?????
110
????????
try
?
{
111
????????????String?filePath?
=
?filePathAndName;
112
????????????filePath?
=
?filePath.toString();
113
????????????File?myFilePath?
=
?
new
?File(filePath);
114
????????????
if
?(
!
myFilePath.exists())?
{
115
????????????????myFilePath.createNewFile();
116
????????????}
117
????????????FileWriter?resultFile?
=
?
new
?FileWriter(myFilePath);
118
????????????PrintWriter?myFile?
=
?
new
?PrintWriter(resultFile);
119
????????????String?strContent?
=
?fileContent;
120
????????????myFile.println(strContent);
121
????????????myFile.close();
122
????????????resultFile.close();
123
????????}
124
????????
catch
?(Exception?e)?
{
125
????????????message?
=
?
"
創建文件操作出錯
"
;
126
????????}
127
????}
128
129
130
????
/**?*/
/**
131
?????*?有編碼方式的文件創建
132
?????*?
@param
?filePathAndName?文本文件完整絕對路徑及文件名
133
?????*?
@param
?fileContent?文本文件內容
134
?????*?
@param
?encoding?編碼方式?例如?GBK?或者?UTF-8
135
?????*?
@return
136
?????
*/
137
????
public
?
void
?createFile(String?filePathAndName,?String?fileContent,?String?encoding)?
{
138
?????
139
????????
try
?
{
140
????????????String?filePath?
=
?filePathAndName;
141
????????????filePath?
=
?filePath.toString();
142
????????????File?myFilePath?
=
?
new
?File(filePath);
143
????????????
if
?(
!
myFilePath.exists())?
{
144
????????????????myFilePath.createNewFile();
145
????????????}
146
????????????PrintWriter?myFile?
=
?
new
?PrintWriter(myFilePath,encoding);
147
????????????String?strContent?
=
?fileContent;
148
????????????myFile.println(strContent);
149
????????????myFile.close();
150
????????}
151
????????
catch
?(Exception?e)?
{
152
????????????message?
=
?
"
創建文件操作出錯
"
;
153
????????}
154
????}
?
155
156
157
????
/**?*/
/**
158
?????*?刪除文件
159
?????*?
@param
?filePathAndName?文本文件完整絕對路徑及文件名
160
?????*?
@return
?Boolean?成功刪除返回true遭遇異常返回false
161
?????
*/
162
????
public
?
boolean
?delFile(String?filePathAndName)?
{
163
?????
boolean
?bea?
=
?
false
;
164
????????
try
?
{
165
????????????String?filePath?
=
?filePathAndName;
166
????????????File?myDelFile?
=
?
new
?File(filePath);
167
????????????
if
(myDelFile.exists())
{
168
?????????????myDelFile.delete();
169
?????????????bea?
=
?
true
;
170
????????????}
else
{
171
?????????????bea?
=
?
false
;
172
?????????????message?
=
?(filePathAndName
+
"
<br>刪除文件操作出錯
"
);
173
????????????}
174
????????}
175
????????
catch
?(Exception?e)?
{
176
????????????message?
=
?e.toString();
177
????????}
178
????????
return
?bea;
179
????}
180
????
181
182
183
????
/**?*/
/**
184
?????*?刪除文件夾
185
?????*?
@param
?folderPath?文件夾完整絕對路徑
186
?????*?
@return
187
?????
*/
188
????
public
?
void
?delFolder(String?folderPath)?
{
189
????????
try
?
{
190
????????????delAllFile(folderPath);?
//
刪除完里面所有內容
191
????????????String?filePath?
=
?folderPath;
192
????????????filePath?
=
?filePath.toString();
193
????????????java.io.File?myFilePath?
=
?
new
?java.io.File(filePath);
194
????????????myFilePath.delete();?
//
刪除空文件夾
195
????????}
196
????????
catch
?(Exception?e)?
{
197
????????????message?
=
?(
"
刪除文件夾操作出錯
"
);
198
????????}
199
????}
200
????
201
????
202
????
/**?*/
/**
203
?????*?刪除指定文件夾下所有文件
204
?????*?
@param
?path?文件夾完整絕對路徑
205
?????*?
@return
206
?????*?
@return
207
?????
*/
208
????
public
?
boolean
?delAllFile(String?path)?
{
209
?????
boolean
?bea?
=
?
false
;
210
????????File?file?
=
?
new
?File(path);
211
????????
if
?(
!
file.exists())?
{
212
????????????
return
?bea;
213
????????}
214
????????
if
?(
!
file.isDirectory())?
{
215
????????????
return
?bea;
216
????????}
217
????????String[]?tempList?
=
?file.list();
218
????????File?temp?
=
?
null
;
219
????????
for
?(
int
?i?
=
?
0
;?i?
<
?tempList.length;?i
++
)?
{
220
????????????
if
?(path.endsWith(File.separator))?
{
221
????????????????temp?
=
?
new
?File(path?
+
?tempList[i]);
222
????????????}
else
{
223
????????????????temp?
=
?
new
?File(path?
+
?File.separator?
+
?tempList[i]);
224
????????????}
225
????????????
if
?(temp.isFile())?
{
226
????????????????temp.delete();
227
????????????}
228
????????????
if
?(temp.isDirectory())?
{
229
????????????????delAllFile(path
+
"
/
"
+
?tempList[i]);
//
先刪除文件夾里面的文件
230
????????????????delFolder(path
+
"
/
"
+
?tempList[i]);
//
再刪除空文件夾
231
????????????????bea?
=
?
true
;
232
????????????}
233
????????}
234
????????
return
?bea;
235
????}
236
237
238
????
/**?*/
/**
239
?????*?復制單個文件
240
?????*?
@param
?oldPathFile?準備復制的文件源
241
?????*?
@param
?newPathFile?拷貝到新絕對路徑帶文件名
242
?????*?
@return
243
?????
*/
244
????
public
?
void
?copyFile(String?oldPathFile,?String?newPathFile)?
{
245
????????
try
?
{
246
????????????
int
?bytesum?
=
?
0
;
247
????????????
int
?byteread?
=
?
0
;
248
????????????File?oldfile?
=
?
new
?File(oldPathFile);
249
????????????
if
?(oldfile.exists())?
{?
//
文件存在時
250
????????????????InputStream?inStream?
=
?
new
?FileInputStream(oldPathFile);?
//
讀入原文件
251
????????????????FileOutputStream?fs?
=
?
new
?FileOutputStream(newPathFile);
252
????????????????
byte
[]?buffer?
=
?
new
?
byte
[
1444
];
253
????????????????
while
((byteread?
=
?inStream.read(buffer))?
!=
?
-
1
)
{
254
????????????????????bytesum?
+=
?byteread;?
//
字節數?文件大小
255
????????????????????System.out.println(bytesum);
256
????????????????????fs.write(buffer,?
0
,?byteread);
257
????????????????}
258
????????????????inStream.close();
259
????????????}
260
????????}
catch
?(Exception?e)?
{
261
????????????message?
=
?(
"
復制單個文件操作出錯
"
);
262
????????}
263
????}
264
????
265
266
????
/**?*/
/**
267
?????*?復制整個文件夾的內容
268
?????*?
@param
?oldPath?準備拷貝的目錄
269
?????*?
@param
?newPath?指定絕對路徑的新目錄
270
?????*?
@return
271
?????
*/
272
????
public
?
void
?copyFolder(String?oldPath,?String?newPath)?
{
273
????????
try
?
{
274
????????????
new
?File(newPath).mkdirs();?
//
如果文件夾不存在?則建立新文件夾
275
????????????File?a
=
new
?File(oldPath);
276
????????????String[]?file
=
a.list();
277
????????????File?temp
=
null
;
278
????????????
for
?(
int
?i?
=
?
0
;?i?
<
?file.length;?i
++
)?
{
279
????????????????
if
(oldPath.endsWith(File.separator))
{
280
????????????????????temp
=
new
?File(oldPath
+
file[i]);
281
????????????????}
else
{
282
????????????????????temp
=
new
?File(oldPath
+
File.separator
+
file[i]);
283
????????????????}
284
????????????????
if
(temp.isFile())
{
285
????????????????????FileInputStream?input?
=
?
new
?FileInputStream(temp);
286
????????????????????FileOutputStream?output?
=
?
new
?FileOutputStream(newPath?
+
?
"
/
"
?
+
287
????????????????????(temp.getName()).toString());
288
????????????????????
byte
[]?b?
=
?
new
?
byte
[
1024
?
*
?
5
];
289
????????????????????
int
?len;
290
????????????????????
while
?((len?
=
?input.read(b))?
!=
?
-
1
)?
{
291
????????????????????????output.write(b,?
0
,?len);
292
????????????????????}
293
????????????????????output.flush();
294
????????????????????output.close();
295
????????????????????input.close();
296
????????????????}
297
????????????????
if
(temp.isDirectory())
{
//
如果是子文件夾
298
????????????????????copyFolder(oldPath
+
"
/
"
+
file[i],newPath
+
"
/
"
+
file[i]);
299
????????????????}
300
????????????}
301
????????}
catch
?(Exception?e)?
{
302
????????????message?
=
?
"
復制整個文件夾內容操作出錯
"
;
303
????????}
304
????}
305
306
307
????
/**?*/
/**
308
?????*?移動文件
309
?????*?
@param
?oldPath
310
?????*?
@param
?newPath
311
?????*?
@return
312
?????
*/
313
????
public
?
void
?moveFile(String?oldPath,?String?newPath)?
{
314
????????copyFile(oldPath,?newPath);
315
????????delFile(oldPath);
316
????}
317
????
318
319
????
/**?*/
/**
320
?????*?移動目錄
321
?????*?
@param
?oldPath
322
?????*?
@param
?newPath
323
?????*?
@return
324
?????
*/
325
????
public
?
void
?moveFolder(String?oldPath,?String?newPath)?
{
326
????????copyFolder(oldPath,?newPath);
327
????????delFolder(oldPath);
328
????}
329
????
public
?String?getMessage()
{
330
????????
return
?
this
.message;
331
????}
332
}
333
334
335
336
??


??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



?35

?36



?37

?38

?39

?40



?41

?42



?43

?44

?45



?46

?47

?48

?49



?50

?51

?52

?53

?54

?55


?56

?57

?58

?59

?60



?61

?62



?63

?64

?65



?66

?67

?68

?69



?70

?71

?72

?73

?74

?75


?76

?77

?78

?79

?80

?81



?82

?83



?84

?85

?86

?87



?88

?89



?90

?91



?92

?93

?94

?95



?96

?97

?98

?99

100

101

102


103

104

105

106

107

108



109

110



111

112

113

114



115

116

117

118

119

120

121

122

123

124



125

126

127

128

129

130


131

132

133

134

135

136

137



138

139



140

141

142

143



144

145

146

147

148

149

150

151



152

153

154

155

156

157


158

159

160

161

162



163

164



165

166

167



168

169

170



171

172

173

174

175



176

177

178

179

180

181

182

183


184

185

186

187

188



189



190

191

192

193

194

195

196



197

198

199

200

201

202


203

204

205

206

207

208



209

210

211



212

213

214



215

216

217

218

219



220



221

222



223

224

225



226

227

228



229

230

231

232

233

234

235

236

237

238


239

240

241

242

243

244



245



246

247

248

249



250

251

252

253



254

255

256

257

258

259

260



261

262

263

264

265

266


267

268

269

270

271

272



273



274

275

276

277

278



279



280

281



282

283

284



285

286

287

288

289

290



291

292

293

294

295

296

297



298

299

300

301



302

303

304

305

306

307


308

309

310

311

312

313



314

315

316

317

318

319


320

321

322

323

324

325



326

327

328

329



330

331

332

333

334

335

336
