zoukankan      html  css  js  c++  java
  • javaIO流实现文件拷贝

    package com.java.demo;
    import java.io.*;
    public class CopyDemo {
        public static void main(String[] args) throws Exception {
            long start = System.currentTimeMillis(); //程序开始的时间
            if(args.length!=2){
                System.out.println("命令错误");
                System.exit(1);//程序结束执行
            }
            File inFile = new File(args[0]); //要读取的文件路径
            if(!inFile.exists()){ //需要拷贝的源文件不存在
                System.out.println("源文件不存在");
                System.exit(2);//程序结束执行
            }
            File outFile = new File(args[1]); //需要写入的文件
            if(!outFile.getParentFile().exists()){ //拷贝文件的父路径不存在
                outFile.getParentFile().mkdirs();//创建父路径
            }
            InputStream input = new FileInputStream(inFile) ;
            OutputStream output = new FileOutputStream(outFile);
            byte data[] = new byte[1024] ;
            int temp = 0 ;
            while((temp = input.read(data)) !=-1){ //读取文件内容,保存在byte数组中,如果读取到最后,则返回-1
                output.write(data,0,temp);//每次读取1024个字节,然后写入目标文件
            }
            input.close();
            output.close();
            long end = System.currentTimeMillis();//程序结束时间
            System.out.println(end-start);
        }
    }
  • 相关阅读:
    第一个自己独立开发并发布的软件
    第一个JavaWeb项目体验
    今天开博
    Mongoid Paging and Iterating Over Large Collections
    图片格式瞎扯淡
    是时候用Coffeescript了
    Mac 小技巧
    印度见闻札记
    作为开发人员,您应该尊重默认行为
    Java 注解(Annoation)学习笔记
  • 原文地址:https://www.cnblogs.com/hu1056043921/p/7388397.html
Copyright © 2011-2022 走看看