文件拷贝
package jess.day13;
import org.junit.Test;
import java.io.*;
public class CopyStream {
public void copyFileStream(String inPath, String outPath) {
InputStream inputStream = null;
OutputStream outputStream = null;
// 容器 每次传输1K
byte[] b = new byte[1024];
int len = 0;
try {
// 构建输入输出对象
inputStream = new FileInputStream(inPath);
outputStream = new FileOutputStream(outPath);
// 读取文件
while ((len = inputStream.read(b)) != -1) {
// 写入文件 每次从b写入 个从0 写到len
outputStream.write(b, 0, len);
System.out.println(len);
}
} catch (IOException e) {
e.printStackTrace();
}
// 关闭资源
finally {
// 判断是否为空
if (inputStream != null) {
try {
// 关闭输入流
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void copyTest() {
String inPath = "E:\谷歌下载\蓝底二寸.png";
String outPath = "D:\360软件管家\蓝底二寸.png";
copyFileStream(inPath, outPath);
}
}
结果