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
package org.uninf.ffmpeg;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * 文件操作类
 * 
 * @author zhangwei
 * 
 * 
 * @time 2013-2-17 上午9:35:50
 * 
 */
public class FileTools {
	static Logger log = LoggerFactory.getLogger(FileTools.class);
 
	/**
	 * 文件复制工具类
	 * 
	 * @param sourceFile
	 * @param targetFile
	 */
	public static final boolean copyFile(String sourceFile, String destFile) {
		long st = System.nanoTime();
		File sf = new File(sourceFile);
		File df = new File(destFile);
		FileChannel fcin = null;
		FileChannel fcout = null;
		try {
			fcin = new FileInputStream(sf).getChannel();
			fcout = new FileOutputStream(df).getChannel();
			fcin.transferTo(0, fcin.size(), fcout);
		} catch (IOException e) {
			log.error(e.getMessage());
			return false;
		} finally {
			try {
				fcin.close();
				fcout.close();
			} catch (IOException e) {
				log.error(e.getMessage());
				return false;
			}
			System.err.println(System.nanoTime() - st);
		}
		return true;
	}
 
	public static final boolean copyFile2(String sourceFile, String destFile) {
		long st = System.nanoTime();
		File sf = new File(sourceFile);
		File df = new File(destFile);
		FileInputStream fin = null;
		FileOutputStream fout = null;
		FileChannel fcin = null;
		FileChannel fcout = null;
		ByteBuffer bb = null;
		long bbSize = 1024 * 1024 * 2;// 缓冲区大小 2M
		try {
			fin = new FileInputStream(sf);
			fout = new FileOutputStream(df);
			fcin = fin.getChannel();
			fcout = fout.getChannel();
			while (true) {
				if (fcin.position() == fcout.size()) {// #复制完毕
					break;
				}
				if (fcin.size() - fcout.position() < bbSize) {
					bbSize = fcin.size() - fcout.position();
				}
				bb = ByteBuffer.allocate((int) bbSize);
				fcin.read(bb);
				bb.flip();
				fcout.write(bb);
				fcout.force(false);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			return false;
		} finally {
			try {
				fcin.close();
				fcout.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.err.println(System.nanoTime() - st);
		}
		return true;
	}
 
	/**
	 * 移动文件
	 * 
	 * @param sourceFile
	 * @param destFile
	 * @return
	 */
	public static boolean removeFile(String sourceFile, String destFile) {
		File source = new File(sourceFile);
		File dest = new File(destFile);
		return source.renameTo(dest);
	}
 
	public static void main(String[] args) {
		FileTools.copyFile2("d:/Tools/jdk-7u10-linux-x64.rpm",
				"e:/jdk-7u10-linux-x64.rpm");
	}
}