Java去除当前目录文件名中包含的空白和空格,并把文件名转变为小写

版权声明:本文为Zhang Phil原创文章,请不要转载! https://blog.csdn.net/zhangphil/article/details/81910743

Java去除当前目录文件名中包含的空白和空格,并把文件名转变为小写

很简单,例如:

import java.io.File;

public class FileApp {
	public static void main(String[] args) {
		File file = new File(".");
		String[] fs = file.list();
		for (int i = 0; i < fs.length; i++) {
			File f = new File(fs[i]);
			String oldName = f.getName();

			// 去除空格和空白。
			oldName = oldName.replaceAll("\\s*", "");

			// 首尾再次裁剪掉空白,并把文件名变为小写。
			String newName = oldName.trim().toLowerCase();

			f.renameTo(new File(newName));
		}
	}
}

猜你喜欢

转载自blog.csdn.net/zhangphil/article/details/81910743
今日推荐