Java学习之IO之File类二

之前学了File便想把我学习视频的名字改了,因为文件名太长不好看,便试着写了个功能实现

 1 package com.gh.file;
 2 
 3 import java.io.File;
 4 
 5 /**
 6  * 批量文件命名
 7  * 
 8  * @author ganhang
 9  * 
10  */
11 public class FileRename {
12     public static String filepath = "F:\\学习\\JAVA\\JAVA_SE";// 要批量重命名的文件夹的地址
13     public static void main(String[] args) {
14         File file = new File(filepath);
15         File[] fileArrays = file.listFiles();
16         // 输出文件数量
17         System.out.println(fileArrays.length);
18         for (File f : fileArrays) 
19         {
20             String filename = f.getName();
21             int len=0;
22             String newfileName = null;
23             if(filename.startsWith("**********JAVA.SE视频")){//避免广告用*代替了
24                 len="**********JAVA.SE视频".length();
25                 newfileName="Java"+filename.substring(len);
26             }
27             else if(filename.startsWith("******_JAVA基础")){
28                 len="******_JAVA基础".length();
29                 newfileName="Java"+filename.substring(len);
30             }
31             f.renameTo(new File(filepath+File.separator+newfileName));//重命名
32         }
33         File[] fileArrays1 = file.listFiles();//输出文件名看看
34         for (File fs : fileArrays1) {
35             System.out.println(fs.getName());
36         }
37     }
38 }

 还有两个作业,补在这后面吧;

 1 package com.gh.homework;
 2 
 3 import java.io.File;
 4 
 5 /**
 6  * 查找指定目录下以指定后缀名结尾的文件;
 7  * 
 8  * @author ganhang
 9  * 
10  */
11 public class FileDemo {
12     public static void findfile(File file, String extsName) {
13         if (file == null)
14             return;
15         if (file.isDirectory()) {
16             File[] fs = file.listFiles();
17             if (fs != null) {
18                 for (File file2 : fs) {
19                     findfile(file2, extsName);// 递归实现;
20                 }
21             }
22         } else {
23             String path = file.getPath();
24             if (path.endsWith(extsName)) {
25                 System.out.println(file.getPath());
26             }
27         }
28     }
29 
30     // 查找d盘下所有txt文件  输出路径;
31     public static void main(String[] args) {
32         File file = new File("d:\\");
33         String extsName = ".txt";
34         findfile(file, extsName);
35     }
36 }
package com.gh.homework;

import java.io.File;

/**
 * 删除指定目录下以指定后缀名结尾的文件;
 * 
 * @author ganhang
 * 
 */
public class FileDeleteDemo {
    public static void deletefile(File file, String extsName) {
        if (file == null)
            return;
        if (file.isDirectory()) {
            if(file.length()==0){
                file.delete();
                 System.out.println("已删除"+file.getPath());
            }
            File[] fs = file.listFiles();
            if (fs != null) {
                for (File file2 : fs) {
                    deletefile(file2, extsName);// 递归实现;
                }
            }
        } else {
            String path = file.getPath().toLowerCase();
            if (path.endsWith(extsName)) {
                //file.delete();
                //System.out.println("已删除"+file.getPath());
            }
        }
    }

    // 删除d盘下所有txt文件  输出路径;
    public static void main(String[] args) {
        File file = new File("f:\\");
        String extsName = ".txt";
        deletefile(file, extsName);
    }
}

 

posted @ 2016-01-23 22:35  CodeNoob  阅读(185)  评论(0编辑  收藏  举报