• Java


    一、文件复制练习

    import java.io.*;
    
    public class CopyStream {
        public void copyStream(File file,String path){
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try {
               fis =  new FileInputStream(file);
               File file1 = new File(path+"\"+file.getName());
               fos = new FileOutputStream(file1);
               byte [] bytes = new byte[1024];
               int count = fis.read(bytes);
               while (count!=-1){
                   fos.write(bytes,0,count);
                   fos.flush();
                   count = fis.read(bytes);
               }
                System.out.println("复制完毕");
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    if(fis!=null) {
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if(fos!=null) {
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
            }
        }
    }
    
    import java.io.File;
    
    public class Test {
        public static void main(String[] args){
            CopyStream cs = new CopyStream();
            cs.copyStream(new File("F:\JavaTest\1\TestTest.txt"),"F:\JavaTest\2");
        }
    }

     二、文件夹复制练习

    import java.io.*;
    
        public void superCopy(File file,String newPath){
    
             String oldFilePath = file.getAbsolutePath();//获取源文件路径
             String newFilePath = newPath+oldFilePath.split(":")[1];//创建新的路径
    
             File newFile = new File(newFilePath);//创建新的对象
    
            File [] newFileArray = file.listFiles();//获取当前传递进来的file对象所有子元素
    
             if(newFileArray != null){//为空则是文件
                 newFile.mkdir();
                 System.out.println(newFile.getName()+"文件夹复制完毕");
                 if(newFileArray.length!=0){
                     for(File f:newFileArray){
                         this.superCopy(f,newPath);
                     }
                 }
             }else{//文件的复制
                 FileInputStream fis = null;
                 FileOutputStream fos = null;
                 try {
                     fis  = new FileInputStream(file);
                     fos = new FileOutputStream(newFile);
                     byte [] bytes =new byte[1024];
                     int count = fis.read(bytes);
                     while (count != -1){
                         fos.write(bytes,0,count);
                         fos.flush();
                         count = fis.read(bytes);
                     }
                     System.out.println(newFile.getName()+"文件复制完毕");
                 } catch (IOException e) {
                     e.printStackTrace();
                 }finally {
                     if(fis == null){
                         try {
                             fis.close();
                         } catch (IOException e) {
                             e.printStackTrace();
                         }
                     }
                     if(fos == null){
                         try {
                             fos.close();
                         } catch (IOException e) {
                             e.printStackTrace();
                         }
                     }
                 }
             }
        }
    }
    
    public class Test {
        public static void main(String[] args){
            CopyStream cs = new CopyStream();
            cs.superCopy(new File("F:\JavaTest"),"F:\JavaTest");
        }
    }
  • 相关阅读:
    Shell脚本定期清空大于1G的日志文件
    Shell脚本实现监视指定进程的运行状态
    Shell脚本实现根据文件的修改时间来分类文件
    通过Shell统计PV和UV
    Python之Html解析方法
    Http请求的Python实现
    Python数据可视化之Pygal(雷达图)
    Python数据可视化之Matplotlib(饼图)
    Python数据可视化之Matplotlib(折线图)
    控制台超市系统(Python)
  • 原文地址:https://www.cnblogs.com/yyanghang/p/11254651.html
Copyright © 2020-2023  润新知