修改文件夹名称

 这个 需要自己测试在使用

package secode.lyp.cmmonclass;

import java.io.File;

/**
 * chartAt(int index),返回某索引的字符
 * trim(),返回字符串的副本,忽略前部空白和后部空白
 * concat()等价于“+”
 * equestIgnoreCase() 忽略大小写比较
 * substring(int beginIndex) 返回一个新的字符串,它是此字符串的beginIndex开始截取
 * substring(int beginIndex,end endIndex),
 * compareTo ()比较两个字符串的大小,负数 前面的小, 
 * endwith()
 * startWith()
 * contains()
 * int IndexOf(String str)
 * int IndexOf(String str,int fromIndex)
 * int lastIndexof(String str)
 * int lastIndexOf(String str,int formIndex)
 * String replace(char oldChar,char newChar)返回一个新的字符串,它是通过newChar替换此字符串中出现的所有oldChar得到的
 * String replace(CharSequence target,CharSequence replacement)使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串
 * String replaceAll(String regex,String replacement) 是要给定的replacement 替换此字符串所有匹配给定的正则表达式的字符串。
 * String replaceFirst(String regex,String replacement):使用给定的replacement替换此字符串匹配给定的正则表达式的第一个字符串。
 * boolean mathches
 * 
 * String[] split(String regex)
 * String[] split (String regex,int limit)
 */

// 修改文件夹名称
public class FileRename {
	
	public static void main(String[] args) {
//		String path = "";
		File fs = new File(path);
		File[] ls = fs.listFiles();
		for (File f:ls) {
		String name = f.getName();
		
		int si = name.indexOf("-");
		int li = name.lastIndexOf("-");
		
		CharSequence sub = name.subSequence(si, li+1);
		String tname = name.replace(sub,"-");
		String parent = f.getParent();
		String rename = parent+"\\"+tname;

		System.out.println(rename);
		
		boolean renameTo = f.renameTo(new File (rename));
		System.out.println(renameTo);
	}
	
	}
	
}

 

Guess you like

Origin blog.csdn.net/qq_35361859/article/details/112515739