FileUtil.java
3.89 KB
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
package com.sw.laryngoscope.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class FileUtil {
public static int CopyFile(String fromFile, String toFile) {
Logger.d(" copyShockFileToSd "+toFile+"--"+fromFile + "length = "+ (new File(fromFile)).length());
FileChannel input = null;
FileChannel output = null;
try {
input = new FileInputStream(new File(fromFile)).getChannel();
output = new FileOutputStream(new File(toFile)).getChannel();
long length = output.transferFrom(input, 0, input.size());
Logger.e(" copyShockFileToSd length = "+length);
} catch (Exception e) {
Logger.w(" error occur while CopySdcardFile", e.toString());
} finally {
try {
input.close();
output.close();
return 0;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return -1;
}
public static boolean copyfile(String fromFile, String toFile) {
File src = new File(fromFile), dst = new File(toFile);
long inSize = src.length();
long outSize = 0;
int progress = 0;
try {
if(dst.exists()){
dst.delete();
}
dst.createNewFile();
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dst);
int length = -1;
byte[] buf = new byte[1024 * 8];
while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
outSize += length;
int temp = (int) (((float) outSize) / inSize * 100);
if (temp != progress) {
progress = temp;
}
}
out.flush();
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static int copyFileToUsb(String fromDir, String toDir) {
//要复制的文件目录
File[] currentFiles;
File root = new File(fromDir);
//如同判断SD卡是否存在或者文件是否存在
//如果不存在则 return出去
if ( !root.exists() || root.list().length ==0 ) {
return -1;
}
//如果存在则获取当前目录下的全部文件 填充数组
currentFiles = root.listFiles();
//目标目录
File targetDir = new File(toDir);
//判断目录是否存在
if ( !targetDir.exists() ) {
targetDir.mkdirs();
}
//遍历要复制该目录下的全部文件
for( int i= 0; i < currentFiles.length; i++ ) {
if ( new File(currentFiles[i].getPath()).isDirectory() )
continue ;
copyfile(currentFiles[i].getPath(), toDir + currentFiles[i].getName());
}
return 0;
}
public static void clearLogFile(String rootDir) {
//要复制的文件目录
File[] currentFiles;
File root = new File(rootDir);
if ( !root.exists() || root.list().length == 0 ) {
return ;
}
currentFiles = root.listFiles();
/*for( int i = 0; i < currentFiles.length; i++ ) {
Logger.d(" currentFiles getPath " + currentFiles[i].getPath());
}*/
if ( currentFiles.length <= 30 ) {
return ;
}
for( int i = 0; i < currentFiles.length - 30; i++ ) {
new File(currentFiles[i].getPath()).delete();
}
}
}