黑马程序员技术交流社区
标题:
复制文件的标准代码,带注释!
[打印本页]
作者:
如初见
时间:
2015-7-9 22:18
标题:
复制文件的标准代码,带注释!
咳咳,,我Q~Q 83332645
public class Test01 {
// 复制MP3加入异常处理的标准代码
public static void main(String[] args) {
Long a = System.currentTimeMillis();
// 创建字节输入流
FileInputStream fileInputStream = null;
// 创建字节输出流
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(new File(
"D:\\英雄时刻\\英雄时刻_20150626-22点59分04s.avi"));// 赋值,给出要写入的路径和文件名
fileOutputStream = new FileOutputStream(new File("copy.avi"));// 赋值,给吃要输出到的路径和文件名,这里是给的默认目录,文件名叫copy.mp3
byte[] byteb = new byte[1024];// 根据 fileInPutStream.read()方法的参数,设定
// byte数组,代表一次读1024个字节
int len = 0;// 根据fileInPutStream 的返回值 给定一个int类型的变量
while ((len = fileInputStream.read(byteb)) != -1) {
fileOutputStream.write(byteb);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 判断fileInPutStream是否为空,如果不空 就close();
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 同上
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Long b = System.currentTimeMillis();
System.out.println((b - a) / 1000);
}
}
复制代码
作者:
如初见
时间:
2015-7-9 22:20
注释是按我自己理解写的,可能有错误,望大家发现了,能提出来~
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2