文件操作工具类FileHandle

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
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package com.vshow.control.tool;

import java.io.BufferedReader;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class FileHandle {

// 删除文件
public static boolean deleteFile(String sPath) {
Boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}

// 删除文件夹(子文件)
public static void delFolder(String folderPath) {

try {

delAllFile(folderPath); // 删除完里面所有内容

String filePath = folderPath;

filePath = filePath.toString();

java.io.File myFilePath = new java.io.File(filePath);

myFilePath.delete(); // 删除空文件夹

} catch (Exception e) {

e.printStackTrace();

}

}

public static boolean delAllFile(String path) {

boolean flag = false;

File file = new File(path);

if (!file.exists()) {

return flag;

}

if (!file.isDirectory()) {

return flag;

}

String[] tempList = file.list();

File temp = null;

for (int i = 0; i < tempList.length; i++) {

if (path.endsWith(File.separator)) {

temp = new File(path + tempList[i]);

} else {

temp = new File(path + File.separator + tempList[i]);

}

if (temp.isFile()) {

temp.delete();

}

if (temp.isDirectory()) {

delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件

delFolder(path + "/" + tempList[i]);// 再删除空文件夹

flag = true;

}

}

return flag;

}

// 获取文件的大小
public static long getFileByte(String filePath) {

long filesize = 0;
File f = new File(filePath);
filesize = f.length();
return filesize;

}

// 拷贝文件夹下所有文件
// path 源文件夹 path1目标文件夹
public static void copy(String path, String path1) throws IOException {

File file = new File(path);

File file1 = new File(path1);

byte[] b = new byte[(int) file.length()];

if (file.isFile()) {
try {
FileInputStream is = new FileInputStream(file);
FileOutputStream ps = new FileOutputStream(file1);
is.read(b);
ps.write(b);
} catch (Exception e) {
// e.printStackTrace();
}
} else if (file.isDirectory()) {
if (!file.exists())
file.mkdir();
String[] list = file.list();
for (int i = 0; i < list.length; i++) {
copy(path + "/" + list[i], path1 + "/" + list[i]);
}
}

}

public static void copy2(String newFile, String oldFile) {
try {

InputStream in = new FileInputStream(newFile);
OutputStream out = new FileOutputStream(oldFile);
byte[] buff = new byte[1024];
int len = 0;
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
in.close();
out.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/** 文件写入数据 **/
public static void readFileInfo(String file, String content)
throws IOException {

File f = new File(file);

FileOutputStream oldFileWriter = new FileOutputStream(f);

oldFileWriter.write(content.getBytes("UTF-8"));

oldFileWriter.close();

}

/**
* 读取txt文件内容
* */
public static String readTxtFile(String filePath) {

String theRule = "";

try {
//String encoding = "utf-8";
String encoding = "";

File file = new File(filePath);

if (file.isFile() && file.exists()) { // 判断文件是否存在

encoding = codeString(filePath);
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
theRule = theRule + lineTxt + "\r\n";
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
return theRule;

}

public static String readFileByLines(String fileName)
throws UnsupportedEncodingException {
File file = new File(fileName);

String returnStr = "";

BufferedReader reader = null;
try {

reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "utf-8"));
String tempString = null;

// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {

returnStr = returnStr + tempString;

}
reader.close();

} catch (IOException e) {

} finally {

if (reader != null) {

try {
reader.close();
} catch (IOException e1) {

}
}
}
return returnStr;
}

/** 文件替换数据 **/
public static void replaceInfos(String url, String oldInfo, String newInfo) {

try {
BufferedReader in_ = new BufferedReader(new FileReader(url));

String line;
String lines = "";
while ((line = in_.readLine()) != null) {

lines = lines + line;

}
in_.close();
lines = lines.replaceAll(oldInfo, newInfo);
File f = new File(url);
FileOutputStream oldFileWriter = new FileOutputStream(f);
oldFileWriter.write(lines.getBytes("UTF-8"));

oldFileWriter.close();
} catch (Exception e) {

}
}

/** 判断文件存在不存在 **/
public static int fileExists(String filePath) {
File file = new File(filePath);
/** 不存在返回0,存在返回1 **/
if (!file.exists()) {
return 0;
} else {
return 1;
}
}

/** 判断文件夹是否存在不存在创建 **/
public static void fileExists_create(String filePath) {
File file = new File(filePath);
/** 不存在返回0,存在返回1 **/
if (!file.exists()) {
try {
file.mkdirs();
} catch (Exception e) {

}
} else {

}
}

public static void copy3(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) {
InputStream inStream = new FileInputStream(oldPath);
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
//System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("error ");
e.printStackTrace();
}
}


/**
* 判断文件的编码格式
* @param fileName :file
* @return 文件编码格式
* @throws Exception
*/
public static String codeString(String fileName) throws Exception{
BufferedInputStream bin = new BufferedInputStream(
new FileInputStream(fileName));
int p = (bin.read() << 8) + bin.read();
String code = null;

switch (p) {
case 0xefbb:
code = "UTF-8";
break;
case 0xfffe:
code = "Unicode";
break;
case 0xfeff:
code = "UTF-16BE";
break;
default:
code = "GBK";
}
return code;
}



/**
* 读取某个文件夹下的所有文件
*/
public static List<String> readfile(String filepath) throws FileNotFoundException, IOException {

List<String> fileZips=new ArrayList<String>();
try {

File file = new File(filepath);

if (!file.isDirectory()) {
//System.out.println("文件");
//System.out.println("path=" + file.getPath());
//System.out.println("absolutepath=" + file.getAbsolutePath());
//System.out.println("name=" + file.getName());
//System.out.println(file.getName());
if(file.getName().indexOf(".zip")!=-1){
System.out.println("name=" + file.getName());
fileZips.add(file.getName());
}


} else if (file.isDirectory()) {
//System.out.println("文件夹");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
if (!readfile.isDirectory()) {
//System.out.println("path=" + readfile.getPath());
//System.out.println("absolutepath="
// + readfile.getAbsolutePath());
//System.out.println("name=" + readfile.getName());

if(readfile.getName().indexOf(".zip")!=-1){
//System.out.println("name=" + readfile.getName());
fileZips.add(readfile.getName());
}

} else if (readfile.isDirectory()) {
readfile(filepath + "\\" + filelist[i]);
}
}

}

} catch (FileNotFoundException e) {
System.out.println("readfile() Exception:" + e.getMessage());
}
return fileZips;
}


}