zoukankan      html  css  js  c++  java
  • 设计一程序(Copy.java),可以实现文件的复制操作

    设计一程序(Copy.java),可以实现文件的复制操作

     

    要求:模仿DOS的copy命令,输入两路径名则实现文件拷贝功能。若输入以下命令Java copy f:\\1.txt f:\\2.txt 则完成把f:\\1.txt的内容复制到f:\\2.txt中。

    提示:可以使用File、FileInputStream、FileOutputStream实现

    package com.cmm.io4;
    
    import java.io.*;
    
    public class Copy {
    
        /**
         * @param arg
         * @Peter Cao
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String filePath1 ="";
            String filePath2 = "";
            File file1 = null;
            File file2 = null;
            byte[] bt = new byte[1024];
            
            if (args.length == 2)
            {
                filePath1 = args[0];
                filePath2 = args[1];
                file1 = new File(filePath1);
                file2 = new File(filePath2);
                
                if (file1.exists() && file1.isFile())
                {
                    try
                    {
                        // FileInputStream -- BufferedInputStream
                        FileInputStream fins = new FileInputStream(file1);
                        BufferedInputStream bins = new BufferedInputStream(fins);
                        // BufferedInputStream bins = new BufferedInputStream(new FileInputStream(file1));                    
                        
                        // FileOutputStream -- BufferedOutputStream
                        FileOutputStream fouts = new FileOutputStream(file2);
                        BufferedOutputStream bouts = new BufferedOutputStream(fouts);
                        
                        if (!file2.exists())
                        {
                            file2.createNewFile(); // 若文件不存在,就创建文件
                        }
                        
    
                        while (bins.read(bt) > 0)  // or bins.read(bt) != -1
                        {
                            bouts.write(bt);
                            bouts.flush();
                            bt = new byte[1024]; // refresh byte[]
                        }
                        
                        bouts.close();
                        fouts.close();
                        bins.close();
                        fins.close();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    }
  • 相关阅读:
    游戏开发中——垂直同步、绘制效率、显示器刷新频率与帧率
    python 异常
    python 多文件知识
    python if,for,while
    python 算术运算
    1.英语单词笔记
    Java import的作用
    java基础点
    Eclipse Java注释模板设置详解
    Java文档注释详解
  • 原文地址:https://www.cnblogs.com/bluestorm/p/2683973.html
Copyright © 2011-2022 走看看