zoukankan      html  css  js  c++  java
  • 遍历目录,查找文件并复制到另一目录

    编写一个程序,把某个目录下所有的带.java文件拷贝到另一个目录中,拷贝成功后,把后缀名是.java改成.txt

     1 import java.io.BufferedReader;
     2 import java.io.BufferedWriter;
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.io.FileOutputStream;
     7 import java.io.IOException;
     8 import java.io.InputStreamReader;
     9 import java.io.OutputStreamWriter;
    10 
    11 public class CopyFileDemo {
    12     public static void main(String[] args) {
    13         File file = new File("D://code");
    14         String suffix = ".java";
    15         findFile(file, suffix);
    16         System.out.println("文件复制成功!");
    17     }
    18     /**
    19      * 遍历目录,找出后缀为.java的文件
    20      * 
    21      * @param file
    22      * @param suffix
    23      */
    24     public static void findFile(File file, String suffix) {
    25         if (file == null)
    26             return;
    27         if (file.isDirectory()) {// 如果file对象是目录
    28             File[] files = file.listFiles();
    29             if (files != null) {
    30                 for (File f : files) {
    31                     findFile(f, suffix);
    32                 }
    33             }
    34         } else {// 如果file对象是文件
    35             //获取jpg路径名
    36             String path = file.getPath().toLowerCase();
    37             //截取jpg文件名前缀
    38             String prefixName = path.substring(path.lastIndexOf("\")+1, path.indexOf("."));
    39             String newFileName = prefixName + ".txt";
    40             if (path.endsWith(suffix)) {
    41                 // 如果文件是java文件,复制到另一个目录
    42                 File targetFile = new File("F:\test" + File.separator + newFileName);
    43                 copyFile(file, targetFile);
    44             }
    45         }
    46     }
    47     /**
    48      * 复制文件到另一个目录,同时修改文件后缀为.txt
    49      * 
    50      * @param sourceFile
    51      * @param targetFile
    52      * @param prefix
    53      * @param suffix
    54      */
    55     public static void copyFile(File sourceFile, File targetFile) {
    56         String line = null;
    57         BufferedReader br = null;
    58         BufferedWriter bw = null;
    59         try {
    60             br = new BufferedReader(new InputStreamReader(new FileInputStream(
    61                     sourceFile)));
    62             bw = new BufferedWriter(new OutputStreamWriter(
    63                     new FileOutputStream(targetFile)));
    64             while ((line = br.readLine()) != null) {
    65                 bw.write(line);
    66                 bw.flush();
    67             }
    68         } catch (FileNotFoundException e) {
    69             e.printStackTrace();
    70         } catch (IOException e) {
    71             e.printStackTrace();
    72         } finally {
    73             try {
    74                 br.close();
    75                 bw.close();
    76             } catch (Exception e) {
    77                 e.printStackTrace();
    78             }
    79         }
    80     }
    81 }
  • 相关阅读:
    Python3中最常用的5种线程锁你会用吗
    学会使用Python的threading模块、掌握并发编程基础
    数据结构与算法Python版 熟悉哈希表,了解Python字典底层实现
    博客导读
    分享canvas的一个小案例
    Php中的魔术方法
    进制简介
    Gojs学习史(一):基本定义
    Vue读书笔记:关于$ref、props和$emit
    Leaflet学习笔记(一)
  • 原文地址:https://www.cnblogs.com/gdpdroid/p/4159383.html
Copyright © 2011-2022 走看看