我的上一篇博客提到了找到文件中的中文。之前的代码还可以做一个扩展。可以作为一个强大的查询功能使用。关键字查询,这个功能大家可以思考一下,今后我的博客也会去贴上这样的关键字查询的功能的代码,能跟大家讨论分析一下。
今天的替换的功能也是基于上篇博客的代码做的一个修改,谈不上什么强大,这个功能性的需求也不是很常见。今天也是跟大家做一个分享吧!
public static void main(String[] args) {
//读取指定文件夹下的所有文件
String filepath = "F:/wk/";//给我你的目录文件夹路径
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("请输入一个目录文件路径");
} else if (file.isDirectory()) {
try {
refreshFileList(filepath);
} catch (IOException e) {
e.printStackTrace();
}
}
}
//递归查找文件
private static void refreshFileList(String strPath) throws IOException {
File files = new File(strPath);
File[] filelist = files.listFiles();
if (filelist == null)
return;
for (int i = 0; i < filelist.length; i++) {
if (filelist[i].isDirectory()) {
refreshFileList(filelist[i].getAbsolutePath());
} else {
String filename = files.getName();//读到的文件名