1. 复制文本文件

  • 复制文本文件,其实就把文本文件的内容从一个文件中读取出来(数据源),然后写入到另一个文件中(目的地)
  • 思路:
    • 根据数据源创建字节输入流对象
    • 根据目的地创建字节输出流对象
    • 读写数据,复制文本文件(一次读取一个字节,一次写入一个字节)
    • 释放资源
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
package com.demo.bytes;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* 复制文本文件
*
* @author jingLv
* @date 2020/12/02
*/
public class CopyTxtDemo {

public static void main(String[] args) throws IOException {
// 根据数据源创建字节输入流对象
FileInputStream fileInputStream = new FileInputStream("java-file-class/javaHello.txt");
// 根据目的地创建输出流对象
FileOutputStream fileOutputStream = new FileOutputStream("java-file-class/javaHelloCopy.txt");
// 读写数据,复制文本(一次读取一个字节,一次写入一个字节)
int by;
while ((by = fileInputStream.read()) != -1) {
fileOutputStream.write(by);
}
// 资源释放
fileInputStream.close();
fileOutputStream.close();
}
}

2.复制图片

思路:

  1. 根据数据源创建字节输入流对象
  2. 根据目的地创建字节输出流对象
  3. 读写数据,复制图片(一次读取一个字节数组,一次写入一个字节数组)
  4. 释放资源
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
package com.demo.bytes;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
* 复制图片
*
* @author jingLv
* @date 2020/12/02
*/
public class CopyImageDemo {
public static void main(String[] args) throws IOException {
// 根据数据源创建字节输入流对象
FileInputStream inputStream = new FileInputStream("java-file-class/image.png");
// 根据目的地创建字节输出流对象
FileOutputStream outputStream = new FileOutputStream("java-file-class/imageCopy.png");
// 读写数据,复制图片(一次读取一个字节数组,一次写入一个字节数组)
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
// 释放资源
inputStream.close();
outputStream.close();
}
}

3.复制视频

思路:

  1. 根据数据源创建字节输入流对象
  2. 根据目的地创建字节输出流对象
  3. 读写数据,复制视频
  4. 释放资源
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
package com.demo.bytes;

import java.io.*;

/**
* 四种方式实现复制视频,并记录每种方式复制视频的时间
* 1. 基本字节流一次读写一个字节
* 2. 基本字节流一次读写一个字节数组
* 3. 字节缓冲流一次读写一个字节
* 4. 字节缓冲流一次读写一个字节数组
*
* @author jingLv
* @date 2020/12/04
*/
public class CopyAviDemo {
public static void main(String[] args) throws IOException {
// 记录开始时间
long startTime = System.currentTimeMillis();
// 复制视频方法一
method1();
// 复制视频方法二
method2();
// 复制视频方法三
method3();
// 复制视频方法四
method4();
// 记录结束时间
long endTime = System.currentTimeMillis();
System.out.println("共耗时:" + (endTime - startTime) + "毫秒");
}

/**
* 字节缓冲流一次读写一个字节数组
*/
private static void method4() throws IOException {
// 数据源
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(""));
// 目的地
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(""));
// 复制
byte[] bytes = new byte[1024];
int len;
while ((len = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, len);
}
// 释放资源
bufferedInputStream.close();
bufferedOutputStream.close();
}

/**
* 字节缓冲流一次读写一个字节
*/
private static void method3() throws IOException {
// 数据源
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(""));
// 目的地
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(""));
// 复制
int by;
while ((by = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(by);
}
// 释放资源
bufferedInputStream.close();
bufferedOutputStream.close();
}

/**
* 基本字节流一次读写一个字节数组
*/
private static void method2() throws IOException {
// 数据源
FileInputStream fileInputStream = new FileInputStream("");
// 目的地
FileOutputStream fileOutputStream = new FileOutputStream("");
// 复制
byte[] bytes = new byte[1024];
int len;
while ((len = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
// 释放资源
fileInputStream.close();
fileOutputStream.close();
}

/**
* 基本字节流一次读写一个字节
*/
private static void method1() throws IOException {
// 数据源
FileInputStream fileInputStream = new FileInputStream("");
// 目的地
FileOutputStream fileOutputStream = new FileOutputStream("");
// 复制
int by;
while ((by = fileInputStream.read()) != -1) {
fileOutputStream.write(by);
}
// 释放资源
fileInputStream.close();
fileOutputStream.close();
}

}