代码功能:
Java实现文件拷贝功能,视频文件Video.wmv从E:/code/Video.wmv拷贝到E:/Content/tttt.wmv。实现算法比较简单。适合新手学习
工具:IntelliJ IDEA
代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 文件Video.wmv从E:/code/Video.wmv拷贝到E:/Content/tttt.wmv
* Created by Administrator on 2017/9/22.
*/
public class TestFileCopy {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
//输出文件不存在会自动新建
try {
fis = new FileInputStream(new File("E:/code/Video.wmv"));
fos = new FileOutputStream(new File("E:/Content/tttt.wmv"));
int b = 0;
//代表每次拷贝的大小设为1024*1/2/4/8/16
byte[] byArr = new byte[1024 * 16];
while ((b = fis.read(byArr)) != -1) {//b指的是读取到的字节数
//System.out.println("b:" + b);
fos.write(byArr, 0, b);//避免最后一次拷贝多余字节
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}