---
title: "JAVA 统一追加扩展名类"
author: "fanjf"
date: "2014-07-07T15:34:08Z"
tags: []
categories: ["开源软件"]
slug: "java"
draft: false
lastmod: "2014-07-07T15:34:08Z"
id: 2089123
url: "https://fanjf.iteye.com/blog/2089123"
---

最近有人反应某云盘，无法上传视频，我也倍受困扰，后来发现MD5 验证不根据文件名识别，故编写一个JAVA 统一追加扩展名类。

package test.file;

import java.io.BufferedWriter;  
import java.io.File;  
import java.io.FileWriter;  
import java.io.PrintWriter;  
import java.util.ArrayList;  
import java.util.List;

import com.util.AbstractObject;

/\**  
 * 1 指定一个文件，统一追加扩展名(如 .txt)  
 * 2 指定一个文件夹，把里面的文件统一追加扩展名(如 .txt)  
 * @author 古色古香  
 * @date 2014-7-7  
 \*/  
public class RenameFileName extends AbstractObject{  
   
 public static String file\_path = "E:/TDDOWNLOAD"; //文件或文件夹路径  
 public static String extension\_name = ".txt";    //扩展名  
   
 //统一输出文件路径  
 public static String output\_FilePath = "C:/Documents and Settings/Administrator/桌面/2.txt";   
   
 public static void main(String\[] args) {  
    
  List filePathList = new ArrayList();//文件名列表  
    
  //获取文件名列表  
  RenameFileName.getFileNameList(file\_path, filePathList);  
    
  showList(filePathList);  
    
  //统一追加扩展名：true 追加，false 删除  
  List newFileList = RenameFileName.renameFileName(filePathList, extension\_name, true);  
    
  //输出追加后文件名列表  
  RenameFileName.appendLineFile(output\_FilePath, newFileList, false);  
    
  System.out.println("=======RenameFileName 执行完毕=========");  
 }

 /\**  
  * 递归取出一个文件的文件名，或者一个文件夹下的文件名列表  
  * @param filePath  
  * @param filePathNameList 文件路径+文件名  
  * @author 古色古香  
  * @date 2014-7-7  
  \*/  
 public static void getFileNameList(String filePath, List filePathNameList) {  
  File f = new File(filePath);  
    
  //当前路径是文件，取得文件名，直接返回  
  if(!f.isDirectory()){  
   //组装文件名  
   filePathNameList.add(f.getPath());  
   return ;  
  }  
    
  // 当前路径是目录，取得File数组  
  File\[] files = f.listFiles();  
  // 当前目录为空，直接返回  
  if(files ==null){  
   System.out.println("目录为空=============="+f.getPath());  
   return ;  
  }else{  
   System.out.println("目录非空=============="+f.getPath()+"，子文件长度+"+files.length);  
  }  
    
  // 循环取出子文件名列表  
  for (int i = 0; i &lt; files.length; i++) {  
   File sonFiles = files\[i];//子文件对象  
   if(sonFiles.isHidden()){  
    System.out.println("隐藏文件===="+sonFiles.getPath() + "\\\\" +sonFiles.getName());  
   }else{  
    //System.out.println("显示文件===="+sonFiles.getPath() + "\\\\" +sonFiles.getName());  
   }  
   // 递归出子目录  
   if (sonFiles.isDirectory()) {  
    System.out.println("子目录路径：" + sonFiles.getPath()+"，子目录是：" + sonFiles.getName());  
      
    // 递归调用，取该目录包含的子文件名  
    getFileNameList(sonFiles.getPath(), filePathNameList);  
   }  
   // 递归出子文件  
   else {  
    System.out.println("子文件名：" + sonFiles.getName());  
    //组装文件名  
    filePathNameList.add(sonFiles.getPath());  
   }  
  }  
 }

    /\**  
  * 统一追加扩展名  
  * @param filePathList 文件路径+文件名  
  * @param extensionName 追加扩展名  
  * @param append  通过这个对象来判断是否向文本文件中追加内容  
  \*/  
 public static List renameFileName(List filePathList,String extensionName, boolean append){  
  List newFileList = new ArrayList();// 新文件路径列表  
    
  if(filePathList == null || filePathList.size()&lt;=0){  
   System.out.println("统一追加扩展名, 源文件路径列表为空：" + filePathList);  
   return newFileList;  
  }  
    
  //循环追加扩展名  
  for(int i=0; i&lt;filePathList.size(); i++){  
   String resFilePath = (String)filePathList.get(i);//源文件名  
     
   //文件名为空，则循环下一个  
   if(resFilePath == null || "".equals(resFilePath)){  
    continue;  
   }  
     
   String newFilePath = resFilePath; //新文件名, 默认不变  
   if(append){  
    if(!resFilePath.endsWith(extensionName)){  
     newFilePath = resFilePath + extensionName;//追加  
    }  
   }else{  
    if(resFilePath.endsWith(extensionName)){  
     newFilePath = resFilePath.substring(0, resFilePath.length()-extensionName.length());//去除  
    }  
   }  
     
   newFileList.add(newFilePath);  
     
   File resFile = new File(resFilePath);  
   File newFile = new File(newFilePath);  
   boolean appednFlg = resFile.renameTo(newFile);  
   if(appednFlg){  
    System.out.println("统一追加扩展名成功：新文件名" + newFilePath);  
   }else{  
    System.out.println("统一追加扩展名失败：源文件名" + resFilePath);  
   }  
  }  
  return newFileList;  
 }  
 /\**  
  * 向文本文件中，逐行重新写入或追加内容  
  * @param filePathName 得到文本文件的路径+文件名  
  * @param fileContent 需要写入的内容  
  * @param append 通过这个对象来判断是否向文本文件中追加内容  
  \*/  
 public static void appendLineFile(String filePathName, List contentList, boolean append) {  
  try {  
   File file = new File(filePathName);  
   if (file.exists() == false){ // 如果文本文件不存在则创建它  
    file.createNewFile();  
    file = new File(filePathName); // 重新实例化  
   }  
     
   //FileWriter是直接跟输出介质发生关系的 ，性能极差  
   FileWriter fileWriter = new FileWriter(file, append);  
     
   //BufferedWriter是输出的时候有一个缓冲区  
   BufferedWriter bufwriter = new BufferedWriter(fileWriter);  
     
   //PrintWriter提供println()方法可以写不同平台的换行符  
   PrintWriter printWriter = new PrintWriter(fileWriter);  
     
   for(int i=0;i&lt;contentList.size();i++){  
    Object fileContent = contentList.get(i);  
    if(fileContent != null){  
//     以下三种方式都可以写  
//     fileWriter.write(fileContent.toString() + "\\n");  
       
//     bufwriter.newLine();  
//     bufwriter.write(fileContent.toString());  
       
     printWriter.println();  
     printWriter.write(fileContent.toString());  
       
    }else{  
     System.out.println("文件内容=========contentList\["+i+"]"+fileContent);  
    }  
   }  
   printWriter.flush();  
   printWriter.close();  
     
//   bufwriter.flush();  
//   bufwriter.close();  
     
   fileWriter.flush();  
   fileWriter.close();  
  } catch (Exception d) {  
   System.out.println(d.getMessage());  
  }  
 }  
}