zoukankan      html  css  js  c++  java
  • 把指定路径中指定后缀名的文件拷贝到另一个路径中

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileOutputStream;
     4 import java.io.InputStream;
     5 import java.io.OutputStream;
     6 
     7 public class CopyFile {
     8 
     9     /**
    10      * @param args
    11      */
    12     private static final  String fileType=".avi";//文件后缀名
    13     public static void main(String[] args) {
    14         
    15         String sourePath="F:\学习文档\Java\传智播客\JDBC\";//源路径
    16         String destPath="F:\学习文档\Java\传智播客\JDBC\";//目的地
    17         File rootFile=new File(sourePath);
    18         showFiles(rootFile,destPath);
    19     }
    20     /**循环输出文件名
    21      * @param file
    22      * @param newPath
    23      */
    24     static  void showFiles(File rootFile,String newPath) {
    25         File[] files= rootFile.listFiles();
    26         for (File file : files) {
    27             
    28             if (file.isDirectory()) {
    29                 showFiles(file,newPath);
    30             }
    31             if (file.getName().endsWith(fileType)) {
    32                 copyFiles(file.getAbsolutePath(),newPath+file.getName());
    33                 //System.out.println(file.getAbsolutePath());
    34                 System.out.println(newPath+file.getName());
    35             }
    36         }
    37     }
    38     /**文件拷贝
    39      * @param oldPath
    40      * @param newPath
    41      */
    42     static void copyFiles(String oldPath,String newPath)
    43     {
    44         try {
    45         int bytesum=0;
    46         int byteread=0;
    47         File oldFile =new File(oldPath);
    48         if (oldFile.exists()) {
    49                 InputStream inputStream=new FileInputStream(oldPath);//获取输入流
    50                 OutputStream outputStream=new FileOutputStream(newPath);//获取输出流
    51                 byte[] buffer=new byte[1444];
    52 
    53                     while ((byteread=inputStream.read(buffer))!=-1) {
    54                         bytesum+=byteread;
    55                         outputStream.write(buffer, 0, byteread);
    56                     }
    57                     outputStream.close();
    58                     inputStream.close();
    59         
    60         }}catch (Exception e) {
    61             System.out.println("文件出错");
    62             e.printStackTrace();
    63         }
    64         
    65     }
    66 }
    View Code
  • 相关阅读:
    chrome的console功能
    vue-cli · Failed to download repo vuejs-templates/webpack: connect ETIMEDOUT 13.229.188.59:443
    let const区别!
    字符串拼接事故!
    git常用的操作命令
    vue项目中利用popstate处理页面返回操作
    Mac下安装nginx并配置SSL实现Https的访问
    浏览器缓存
    HTTP相关知识点
    CORS请求
  • 原文地址:https://www.cnblogs.com/changshuo/p/3244222.html
Copyright © 2011-2022 走看看