批量转换文件扩展名

今天学习了feiy(email:eshangrao@gmail.com  http://blog.eshangrao.com/)做的批量修改文件目录下的文件扩展名 的代码. 自己根据理解的程度,修改了一些, 在此做个记录,算是学习小结,呵呵:)

import  java.io. * ;
import  java.util. * ;


public   class  RenameExts  ... {
    
public RenameExts() ...{
        
// TODO Auto-generated constructor stub
    }

        
private String orgExt;
        
private String newExt;
        
private boolean debug=false;
        
private int allFileNum;
        
private int sucFileNum;
        
private int failFileNum;
        
public RenameExts(String dir,String orgExt,String newExt,boolean debug)...{
            
this.orgExt=orgExt;
            
this.newExt=newExt;
            
this.debug=debug;       
            changeName(dir);
            System.out.println(
"--------------------------------------");
            System.out.println(
"转换扩展名:"+orgExt+"到:"+newExt+"完毕!");
            System.out.print(
"   需要修改的文件:"+allFileNum);       
            System.out.println(
",成功:"+sucFileNum+",失败:"+failFileNum);
            System.out.println(
"--------------------------------------");
        }

       
        
private void changeName(String cPath)...{
            File class_path
=new File(cPath);
            
//如果指定路径不存在,抛出信息,并退出
            if(!class_path.exists())...{
                System.out.println(
"The path doesn't exist");
                System.exit(
-1);
                }

            
//如果是文件,修改后缀
            if (class_path.isFile())...{
                
int lastIndex=class_path.getName().lastIndexOf(".");
                String spname
=class_path.getName().substring(0,lastIndex);
                String spext
=class_path.getName().substring(lastIndex+1);               
                
if(spext.equals(orgExt))...{
                    allFileNum
++;
                    String newFileName
=class_path.getParent()+File.separator+spname+"."+newExt;
                    
boolean result=class_path.renameTo(new File(newFileName));                    
                    
if(result)...{
                         sucFileNum
++;    
                    }
else...{
                        failFileNum
++;    
                    }
                                   
                    
if(debug)...{System.out.println("rename:"+cPath+" to: "+newFileName+","+result);}
                    }
else...{
                        System.out.println(cPath
+ "has different extend !");
                    }

            }

            
//如果是文件夹,递归调用,查找下面的文件 
            else...{              
                String[] list
=class_path.list();
                
if(debug)
                    System.out.println(
"change to path:"+cPath);
                
for(int i=0;i<list.length;i++)...{
                    String list_item
=list[i];
                    String fileName
=class_path.getPath()+File.separator+list_item;
                    changeName(fileName);       
                }

            }

        }

        
public static void main(String[] args) ...{
            
try...{
                System.out.println(
"Input the path like:");
                System.out.println(
"需要更改扩展名的文件/文件夹路径 原始扩展名 新的扩展名 [是否调试:1/0]");
                BufferedReader br
=new BufferedReader(new InputStreamReader(System.in));
                String s
=br.readLine();
                String[] l
=s.split(" ");
                
if(l.length<3)...{
                    System.out.println(
"Usage:");
                    System.out.println(
"需要更改扩展名的文件/文件夹路径 原始扩展名 新的扩展名 [是否调试:1/0]");
                    System.exit(
-1);
                }
else...{
                    
if(l.length>=4)...{
                        
new RenameExts(l[0],l[1],l[2],l[3].equals("1"));
                    }
else...{
                        
new RenameExts(l[0],l[1],l[2],false);
                    }

                }

               
            }
catch(Exception e)...{
                e.printStackTrace();
            }


        }


    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值