- 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();
- }
- }
- }
-
- }
- }
复制代码 |
|