1 public class FileUtils {
2 /**
3 * 遍历目录 遍历出目录下的文件名和子目录名
4 *
5 * @param dir 目录
6 * @throws IllegalAccessException
7 */
8 public static void listDirectory(File dir) throws IllegalAccessException {
9 if (!dir.exists()) {
10 throw new IllegalAccessException("目录:" + dir + "不存在");
11 }
12 if (!dir.isDirectory()) {
13 throw new IllegalAccessException(dir + "不是目录");
14 }
15 String[] fileNames = dir.list();
16 for (String s : fileNames) {
17 System.out.println(dir + "//" + s);
18 }
19 }
20
21 /**
22 * 一个字节一个字节的复制文件
23 *
24 * @param srcFile
25 * @param destFile
26 * @throws IllegalAccessException
27 * @throws IOException
28 */
29 public static void copyFileByByte(File srcFile, File destFile) throws IllegalAccessException, IOException {
30 if (!srcFile.exists()) {
31 throw new IllegalAccessException("文件:" + srcFile + "不存在");
32 }
33 if (!srcFile.isFile()) {
34 throw new IllegalAccessException(srcFile + "不是文件");
35 }
36 FileInputStream in = new FileInputStream(srcFile);
37 FileOutputStream out = new FileOutputStream(destFile);
38 int b;
39 while ((b = in.read()) != -1) {
40 out.write(b);
41 out.flush();
42 }
43 in.close();
44 out.close();
45 }
46
47 /**
48 * 批量复制文件
49 *
50 * @param srcFile 源文件对象
51 * @param destFile 复制到的文件对象
52 * @throws IllegalAccessException
53 * @throws FileNotFoundException
54 */
55 public static void copyFile(File srcFile, File destFile) throws IllegalAccessException, IOException {
56 if (!srcFile.exists()) {
57 throw new IllegalAccessException("文件:" + srcFile + "不存在");
58 }
59 if (!srcFile.isFile()) {
60 throw new IllegalAccessException(srcFile + "不是文件");
61 }
62 FileInputStream in = new FileInputStream(srcFile);
63 FileOutputStream out = new FileOutputStream(destFile);
64 byte[] buf = new byte[8 * 1024];
65 int b;
66 while ((b = in.read(buf, 0, buf.length)) != -1) {
67 out.write(buf, 0, b);
68 out.flush();
69 }
70 in.close();
71 out.close();
72 }
73
74 /**
75 * 复制文件 利用缓冲区
76 *
77 * @param srcFile
78 * @param destFile
79 * @throws IllegalAccessException
80 * @throws IOException
81 */
82 public static void copyFileByBuffer(File srcFile, File destFile) throws IllegalAccessException, IOException {
83 if (!srcFile.exists()) {
84 throw new IllegalAccessException("文件:" + srcFile + "不存在");
85 }
86 if (!srcFile.isFile()) {
87 throw new IllegalAccessException(srcFile + "不是文件");
88 }
89 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
90 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
91 int c = 0;
92 while ((c = bis.read()) != -1) {
93 bos.write(c);
94 //刷新缓存区
95 bos.flush();
96 }
97 bis.close();
98 bos.close();
99 }
100
101 /**
102 * 批量缓存复制文件
103 *
104 * @param srcFile
105 * @param destFile
106 * @throws IllegalAccessException
107 * @throws IOException
108 */
109 public static void copyFileBatchBuffer(File srcFile, File destFile) throws IllegalAccessException, IOException {
110 if (!srcFile.exists()) {
111 throw new IllegalAccessException("文件:" + srcFile + "不存在");
112 }
113 if (!srcFile.isFile()) {
114 throw new IllegalAccessException(srcFile + "不是文件");
115 }
116 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
117 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
118 byte[] buf = new byte[8 * 1024];
119 int b;
120 while ((b = bis.read(buf, 0, buf.length)) != -1) {
121 bos.write(buf, 0, b);
122 bos.flush();
123 }
124 bis.close();
125 bos.close();
126 }
127
128 /**
129 * 复制 1.76M MP4文件用时比较
130 *
131 * @param args
132 * @throws IllegalAccessException
133 * @throws IOException
134 */
135 public static void main(String[] args) throws IllegalAccessException, IOException {
136 long startTime = System.currentTimeMillis();
137 //单字节复制用时:6596ms
138 copyFileByByte(new File("D:\\文件\\QQ视频20180718111001.mp4"), new File("D:\\文件\\1.mp4"));
139 //批量复制:6ms
140 copyFile(new File("D:\\文件\\QQ视频20180718111001.mp4"), new File("D:\\文件\\2.mp4"));
141 //单字节缓存复制用时:2709ms
142 copyFileByBuffer(new File("D:\\文件\\QQ视频20180718111001.mp4"), new File("D:\\文件\\3.mp4"));
143 //批量缓存复制用时:5ms
144 copyFileBatchBuffer(new File("D:\\文件\\QQ视频20180718111001.mp4"), new File("D:\\文件\\4.mp4"));
145 long endTime = System.currentTimeMillis();
146 System.out.println(endTime - startTime);
147 }
148 }