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());
}
}
}