黑马程序员技术交流社区
标题:
字节输入输出流
[打印本页]
作者:
chenguoyu520
时间:
2015-10-3 14:48
标题:
字节输入输出流
import java.io.*;
class InputAndOutputDemo {
public static void main(String[] args) {
//创建文件对象
File source=new File("E:\\超能陆战队.rmvb");
File target=new File("E:\\超能陆战队.rmvb");
if(!target.exists()){//如果目标文件不存在,则创建
try {
target.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileInputStream in=null;
FileOutputStream out=null;
try {
//创建字节输入流对象
in=new FileInputStream(source);
//创建字节输出流对象
out=new FileOutputStream(target);
byte[] buf=new byte[1024];//定义字节数组
int len;
while((len=in.read(buf))!= -1){
out.write(buf, 0, len);
}
out.flush();//刷新
} catch (Exception e) {
e.printStackTrace();
}finally{//关闭资源
if(in !=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2