import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
public class fileBP implements string_Processing
{
public static String ENCODING="UTF-8";
public static void main(String args[])
{
//Scanner sc = new Scanner(System.in,"GBK");
Scanner sc = new Scanner(System.in,ENCODING);
System.out.println("请输入预计访问的路径");
String path=(sc.nextLine()).trim();
System.out.println(showMessage.getInfo());
while(true)
{
new fileBP().sP( (sc.nextLine()).trim(),path);
}
}
@Override
public void sP(String parm, String path)
{
String s[];//临时变量,用于分割参数
File file;//临时变量
String name;//临时变量
List<File> list=pP(path);
Scanner sc = new Scanner(System.in,ENCODING);
if(list.size()>0)
{
file = list.get(0);
name =file.getName();
StringBuilder sb;
if(parm.contains("-"))
{
s=parm.split("-");
//-结果
if(Pattern.compile("^\\-((?!\\-).)+").matcher(parm).matches())
{
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append(",修改后文件名:").append(name.substring(0,name.indexOf(s[1])+s[1].length()));
sb.append(",确认是否继续,输入Y表示继续:");
System.out.println(sb);
if( "Y".equals((sc.nextLine()).trim()))
{
for(int i=0;i<list.size();i++)
{
sb = new StringBuilder();
file = list.get(i);
name =file.getName();
if(!file.renameTo(new File(file.getParent()+File.separator+name.substring(0,name.indexOf(s[1])+s[1].length()))))
{
System.out.println("更改失败!");
}
sb.append("原文件名:").append(name);
sb.append(",修改后文件名:").append(name.substring(0,name.indexOf(s[1])+s[1].length()));
sb.append(",原路径:").append( file.getParent()).append(File.separator).append(name);
sb.append(",修改后:").append( file.getParent()).append(File.separator).append(name.substring(0,name.indexOf(s[1])+s[1].length()));
System.out.println(sb);
}
}
}
//结果-
if(Pattern.compile("((?!\\-).)+\\-$").matcher(parm).matches())
{
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append(",修改后文件名:").append( name.substring(name.indexOf(s[0]),name.length()));
sb.append(",确认是否继续,输入Y表示继续:");
System.out.println(sb);
if( "Y".equals((sc.nextLine()).trim()))
{
for(int i=0;i<list.size();i++)
{
file = list.get(i);
name =file.getName();
if(!file.renameTo(new File(file.getParent()+File.separator+name.substring(name.indexOf(s[0]),name.length()))))
{
System.out.println("更改失败!");
}
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append(",修改后文件名:").append(name.substring(name.indexOf(s[0]),name.length()));
sb.append(",原路径:").append( file.getParent()).append(File.separator).append(name).append(",修改为:").append(file.getParent()).append(File.separator).append(name.substring(name.indexOf(s[0]),name.length()));
System.out.println(sb);
}
}
}
//4-打印
if(Pattern.compile("^[0-9]+\\-((?!\\-).)+").matcher(parm).matches())
{
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append("修改后文件名:").append( name.substring(0,Integer.parseInt(s[0]))).append(s[1]).append(name.substring(Integer.parseInt(s[0]),name.length()));
sb.append("确认是否继续,输入Y表示继续:");
System.out.println(sb);
if( "Y".equals((sc.nextLine()).trim()))
{
for(int i=0;i<list.size();i++)
{
file = list.get(i);
name =file.getName();
if(!file.renameTo(new File(file.getParent()+File.separator+ name.substring(0,Integer.parseInt(s[0]))+s[1]+name.substring(Integer.parseInt(s[0]),name.length()))))
{
System.out.println("更改失败!");
}
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append("修改后文件名:").append( name.substring(0,Integer.parseInt(s[0]))).append(s[1]).append(name.substring(Integer.parseInt(s[0]),name.length()));
sb.append("原路径:").append( file.getParent()).append(File.separator).append(name).append(",修改为:").append(file.getParent()).append(File.separator).append( name.substring(0,Integer.parseInt(s[0]))).append(s[1]).append(name.substring(Integer.parseInt(s[0]),name.length()));
System.out.println(sb);
}
}
}
//1-2-a
if(Pattern.compile("^[0-9]+\\-[0-9]+\\-((?!\\-).)+").matcher(parm).matches())
{
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append(",修改后文件名:").append( name.replace(name.substring(Integer.parseInt(s[0]),Integer.parseInt(s[1])),s[2]));
sb.append(",确认是否继续,输入Y表示继续:");
System.out.println(sb);
if( "Y".equals((sc.nextLine()).trim()))
{
for(int i=0;i<list.size();i++)
{
file = list.get(i);
name =file.getName();
if(!file.renameTo(new File(file.getParent()+File.separator+ name.replace(name.substring(Integer.parseInt(s[0]),Integer.parseInt(s[1])),s[2]))))
{
System.out.println("更改失败!");
}
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append(",修改后文件名:").append( name.replace(name.substring(Integer.parseInt(s[0]),Integer.parseInt(s[1])),s[2]));
sb.append(",原路径:").append( file.getParent()).append(File.separator).append(name).append(",修改为:").append(file.getParent()).append(File.separator).append( name.replace(name.substring(Integer.parseInt(s[0]),Integer.parseInt(s[1])),s[2]));
System.out.println(sb);
}
}
}
}
if(parm.contains("--"))
{
s=parm.split("--");
//a--b
if(Pattern.compile("((?!\\-).)+\\-\\-((?!\\-).)+").matcher(parm).matches())
{
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append("修改后文件名:").append(name.replace(s[0], s[1]));
sb.append("确认是否继续,输入Y表示继续:");
System.out.println(sb);
if( "Y".equals((sc.nextLine()).trim()))
{
for(int i=0;i<list.size();i++)
{
file = list.get(i);
name =file.getName();
if(!file.renameTo(new File(file.getParent()+File.separator+ name.replace(s[0], s[1]))))
{
System.out.println("更改失败!");
}
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append("修改后文件名:").append( name.replace(s[0], s[1]));
sb.append("原路径:").append( file.getParent()).append(File.separator).append(name).append(",修改为:").append(file.getParent()).append(File.separator).append( name.replace(s[0], s[1]));
System.out.println(sb);
}
}
}
//1-5-000
if(Pattern.compile("^[0-9]+\\-\\-[0-9]+\\-\\-[0]*").matcher(parm).matches())
{
System.out.println("请输入预计排序的依据,格式为2-4,程序会排除其中的非数字部分:");
String s1[]=(sc.nextLine()).trim().split("-");//排序
int k=name.length();
String str;
if(s1.length>1)
k=Integer.parseInt(s1[1]);
if(s.length>2)
str=new DecimalFormat(s[2]).format(Integer.parseInt(s[1]));
else
str=s[1];
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append("修改后文件名:").append(name.substring(0,Integer.parseInt(s[0]))).append(str).append(name.substring(Integer.parseInt(s[0]),name.length()));
sb.append("排序数:").append(Pattern.compile("[^0-9]").matcher(name.substring(Integer.parseInt(s1[0]),k)).replaceAll(" ").trim());
sb.append("确认是否继续,输入Y表示继续:");
System.out.println(sb);
if( "Y".equals((sc.nextLine()).trim()))
{
List <duplicateRemoval>list1=xupp(list,s1);
for(int i=0;i<list1.size();i++)
{
file = list1.get(i).getpath();
name =file.getName();
k=name.length();
if(s1.length>1)
k=Integer.parseInt(s1[1]);
if(s.length>2)
str=new DecimalFormat(s[2]).format(Integer.parseInt(s[1])+i);
else
str=(Integer.parseInt(s[1])+i)+"";
if(!file.renameTo(new File(file.getParent()+File.separator+ name.substring(0,Integer.parseInt(s[0]))+str+name.substring(Integer.parseInt(s[0]),name.length()) )))
{
System.out.println("更改失败!");
}
sb = new StringBuilder();
sb.append("原文件名:").append(name);
sb.append("排序数:").append(Pattern.compile("[^0-9]").matcher(name.substring(Integer.parseInt(s1[0]),k)).replaceAll(" ").trim());
sb.append("原路径:").append( file.getParent()).append(File.separator).append(name).append(",修改为:").append(file.getParent()).append(File.separator).append( name.substring(0,Integer.parseInt(s[0]))).append(str).append(name.substring(Integer.parseInt(s[0]),name.length()));
System.out.println(sb);
}
}
}
}
if("show".equals(parm.trim()))
{
System.out.println("请输入预计排序的依据,格式为2-4,程序会排除其中的非数字部分:");
String s1[]=(sc.nextLine()).trim().split("-");//排序
List <duplicateRemoval>list1=xupp(list,s1);
for(int i=0;i<list1.size();i++)
{
System.out.println(list1.get(i).getpath());
}
}
if("copy".equals(parm.trim()))
{
System.out.println("请输入预计排序的依据,格式为2-4,程序会排除其中的非数字部分:");
String s1[]=(sc.nextLine()).trim().split("-");//排序
List <duplicateRemoval>list1=xupp(list,s1);
System.out.println("拷贝的路径:");
String ph=sc.nextLine().trim();
File f0;
for(int i=0;i<list1.size();i++)
{
f0=list1.get(i).getpath();
fileChannelCopy(f0,new File(ph+File.separator+f0.getName()));
System.out.println("预计拷贝文件数为:"+list.size()+",当前拷贝到第"+(i+1)+"个,文件路径及文件名为:"+f0);
}
}
}
if("exit".equals(parm.trim()))
{
System.exit(0);
}
System.out.println(showMessage.getInfo());
}
@Override
public List<File> pP(String path)
{
File file[]=new File(path).listFiles();
List <File>list = new ArrayList<>();
for (File file1 : file)
if (file1.isFile())
list.add(file1);
return list;
}
@Override
public List xupp(List <File>list,String s1[])
{
File file;
String name;
int k;
List <duplicateRemoval>list1 = new ArrayList<>();
for(int i=0;i<list.size();i++)
{
file = list.get(i);
name =file.getName();
k=name.length();
if(s1.length>1)
k=Integer.parseInt(s1[1]);
list1.add(new duplicateRemoval(Pattern.compile("[^0-9]").matcher(name.substring(Integer.parseInt(s1[0]),k)).replaceAll(" ").trim(),file));
}
Collections.sort(list1);
for(int i=0;i<list1.size();i++)
{
System.out.println("排序结果:"+list1.get(i).getpath());
}
return list1;
}
@Override
public void fileChannelCopy(File s, File t)
{
FileChannel in = null;
try {
in = new FileInputStream(s).getChannel();//得到对应的文件通道
in.transferTo(0, in.size(), new FileOutputStream(t).getChannel());//连接两个通道,并且从in通道读取,然后写入out通道
} catch (FileNotFoundException ex) {
Logger.getLogger(fileBP.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(fileBP.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
try {
if(in!=null)
in.close();
}
catch (IOException ex) {
Logger.getLogger(fileBP.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
interface string_Processing //方法申明
{
void sP(String parm,String path);//根据参数,文件路径返回文件路径
List pP(String path);//获取文件
List xupp(List <File>list,String s1[]);
void fileChannelCopy(File s, File t) ;
}
class showMessage//提示信息显示
{
private static StringBuilder Info;
public static String getInfo()
{
Info = new StringBuilder();//提示语句拼接
Info.append("请按提示格式输入参数,参考文件名为:“测试输入结果看下.mp4”\r\n");
Info.append(" 1.文件名称右截取,输入参数“-结果”,得到文件名称:“测试输入结果”\r\n");
Info.append(" 2.文件名称左截取,输入参数“结果-”,得到文件名称:“结果看下.mp4”\r\n");
Info.append(" 3.指定位置插入字符串,输入参数“4-打印”,得到文件名称:“测试输入打印结果看下.mp4”\r\n");
Info.append(" 4.指定位置字符串替换,输入参数“2-7-看”,得到文件名称:“测试看下.mp4”\r\n");
Info.append(" 5.字符串替换(中间双横杠),输入参数“输入结果看--看”,得到文件名称:“测试看下.mp4”\r\n");
Info.append(" 6.指定位置插入指定格式的数字(中间双横杠),输入参数”1--10--000“,文件名第1个字后面插入从10开始的数字,并且把数字格式化为000”\r\n");
Info.append("ps:\r\n");
Info.append(" 1.输入show,表示显示当前文件的排列顺序\r\n");
Info.append(" 1.输入copy,表示需要拷贝文件\r\n");
Info.append(" 3.输入exit表示退出程序\r\n");
return Info.toString();
}
}
class duplicateRemoval implements Comparable<duplicateRemoval>//去重复
{
private final String str;//比较值
private final File path;//路径
public duplicateRemoval(String str,File path)
{
this.str=str;
this.path=path;
}
public String getstr()//只去字符串中的数字部分
{
return str;
}
public File getpath()//只去字符串中的数字部分
{
return path;
}
@Override
public int compareTo(duplicateRemoval o)
{
return new java.math.BigDecimal(this.str).subtract(new java.math.BigDecimal(o.getstr())).intValue();
}
}原文:http://lxpandyxl.blog.51cto.com/4274978/1857403