zoukankan      html  css  js  c++  java
  • 《Java基础》___使用字节流复制文件

       以前在学校,为了准备某个证书考试,预习的时候写的。没什么技术含量,主要是熟悉一下文件流的基本操作。

    Example:

     **************************************************************************************************************************

    import java.io.*;
    import java.util.Scanner;

    /**
    * java使用字节流复制图像
    *
    @author Administrator
    *@time 2011年6月9日 19:55:10
    */
    public class MyTest2 {
    private String filename; //文件名
    private double filesize; //文件大小
    public static void main(String agrs[]){
    MyTest2 tt2=new MyTest2();
    String frompath="E:\\qq.png";
    String topath="F:\\qq.png";
    if(tt2.CheckFile(frompath, topath)){
    tt2.CopyFile(frompath, topath);
    }
    }
    //复制文件
    public void CopyFile(String frompath,String topath){
    File file1=new File(frompath);//源文件路径
    File file2=new File(topath);//目标文件路径
    filename=file1.getName();
    filesize=(file1.length())/1024/1024;
    System.out.println("********************文件属性********************");
    System.out.println("源文件路径:"+frompath);
    System.out.println("目标文件路径:"+topath);
    System.out.println("文件名称:"+filename);
    System.out.println("文件大小:"+filesize+" MB");
    int ch=0;
    try{
    FileInputStream fin=new FileInputStream(file1);
    FileOutputStream fout=new FileOutputStream(file2);
    ch=fin.read();
    System.out.println("开始复制!");
    long startTime=System.currentTimeMillis(); //获取开始时间
    while(ch!=-1){
    fout.write(ch);
    ch=fin.read();
    }
    long endTime=System.currentTimeMillis(); //获取结束时间
    System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
    System.out.println("复制完毕!");
    //关闭流
    fin.close();
    fout.close();
    }catch(Exception e){
    System.err.println("Error: "+e);
    }
    }
    //验证文件是否存在
    public boolean CheckFile(String frompath,String topath){
    File file1=new File(frompath);//源文件路径
    File file2=new File(topath);//目标文件路径
    if(!file1.exists()){ //文件不存在
    System.out.println("源文件不存在,请检查路径是否正确!");
    return false;
    }else{
    if(file2.exists()){
    System.out.println("目标文件已经存在,请选择 覆盖/取消 ?");
    System.out.println("1: 覆盖 2:取消");
    try{
    Scanner sc=new Scanner(System.in);
    int a=sc.nextInt();
    if(a==1){
    System.out.println("你输入的是1,操作将继续,目标文件将被覆盖。");
    return true;
    }else if(a==2){
    System.out.println("您输入了2,操作将取消。");
    return false;
    }else{

    System.out.println("输入无效。。;");
    CheckFile(frompath, topath);

      return false;

             }

    }

         catch(Exception ee){
    System.out.println("输入无效。。;");
    CheckFile(frompath, topath);
    return false;
    }
    }
    }
    return false;
    }
    }



     

    作者:北羽
    出处:http://www.cnblogs.com/whynever
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接
    如有问题,可以通过418537487@qq.com 联系我,非常感谢。

  • 相关阅读:
    android中数据存储
    服装销售系统数据库课程设计(MVC)
    重新设计的道道指令
    CSS cursor属性
    hdu4488 Faulhaber’s Triangle(模拟题)
    SPOJ 1043 1043. Can you answer these queries I
    再谈内存管理与ARC运行机制(一)
    PowerMock注解PowerMockIgnore的使用方法
    hdu 1317 XYZZY【Bellheman_ford 判断正环小应用】
    HDU 1104 Remainder( BFS(广度优先搜索))
  • 原文地址:https://www.cnblogs.com/whynever/p/2291163.html
Copyright © 2011-2022 走看看