public boolean copyFile(
String sourceFile,
String distFile
) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(sourceFile));
bos = new BufferedOutputStream(new FileOutputStream(distFile));
int readLen;
byte[] buf = new byte[8192];
while ((readLen = bis.read(buf)) > -1) {
bos.write(buf, 0, readLen);
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
if (bis != null) {
bis.close();
}
if (bos != null) {
bos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
- 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
public void copyDir(
String oldDir,
String newDir
) {
File newDirFile = new File(newDir);
if (!newDirFile.exists()) {
newDirFile.mkdirs();
}
File f1 = new File(oldDir);
File[] files = f1.listFiles();
String newTarget;
for (File ff : files) {
if (!ff.isDirectory()) {
newTarget = newDir + File.separator + ff.getName();
copyFile(ff.getAbsolutePath(), newTarget);
}
else {
copyDir(oldDir + File.separator + ff.getName(), newDir + File.separator + ff.getName());
}
}
}
- 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