一次性修改大量文件的后缀名第一版

package com.ljw.wuyaowang1;

import javax.swing.*;
import java.io.*;

/**
 * 需求:
 * 一次性修改文件的后缀名  1.0.0.0版
 * 将位于D:\software\学习文件\测试\wuyaowang\imgCache文件夹下的文件名字取出来,修改后缀为img
 */
public class img_test {
    public static void main(String[] args) {
        Long startTime=System.currentTimeMillis();
        File file=new File("D:\\software\\学习文件\\测试\\wuyaowang\\image\\");
        File file1=new File("D:\\ljw\\work_projects\\20208\\测试\\巫妖王\\");
        img_test imgTest=new img_test();
        System.out.println(file1.getAbsoluteFile());
//        imgTest.changImg(file);
        imgTest.removejpg(file1,file);
        Long endTime=System.currentTimeMillis();
        System.out.println("程序运行时间为"+"["+(endTime-startTime)+"]");
    }

    public void removejpg(File file1,File file2){

        if(file1.isDirectory()){
          File[] files=file1.listFiles();
          for(File f:files){
              if(f.getName().endsWith("jpg")){
                  FileOutputStream fileOutputStream=null;
                  FileInputStream fileInputStream=null;

                  try {
                      fileInputStream=new FileInputStream(file1.getAbsoluteFile()+"\\"+f.getName());

                      fileOutputStream=new FileOutputStream(file2.getAbsoluteFile()+"\\"+f.getName());
                      byte[] bytes=new byte[1024];
                      int temp=0;
                      while((temp=fileInputStream.read(bytes))!=-1){
                          fileOutputStream.write(bytes,0,temp);
                          fileOutputStream.flush();
                      }


                  } catch (FileNotFoundException e) {
                      e.printStackTrace();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }finally {
                      try {
                          fileOutputStream.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                      try {
                          fileInputStream.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                      f.delete();
                  }
              }

            }
        }
    }
    public void changImg(File file){
        //1.使用file类找到一个文件

        if(file.isDirectory()){
            File[] files = file.listFiles();
            for(File f:files){
                if(f.isDirectory()){
                    System.out.println("目录"+f.getPath());
                     changImg(file);
                }else{
                    String oldname=new String(f.getName());
                    String SubString=oldname.substring(oldname.lastIndexOf(".")+1);
                    String newname=oldname.replace(SubString,"jpg");
                    boolean b = f.renameTo(new File(newname));


//                    System.out.println("文件"+f.getName());
//                    String s=new String(f.getName());
//                    System.out.println(s);
//                    String substring = s.substring(s.lastIndexOf(".") + 1);
//                    System.out.println(substring);
//                    String img = s.replace(substring, "img");
//                    System.out.println(img);
                }
            }
        }else{
            System.out.println("文件"+file.getPath());
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值