一个替换某文件夹下的所有文件中特定字符串的小工具
来源:互联网 发布:优盘格式化恢复软件 编辑:程序博客网 时间:2024/06/05 06:07
这里与大家分享一个替换文件中某字符串的小程序,经测试完全可用,希望对大家有帮助
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class ReplaceWordUtil {
public static void main(String[] args) {
/**
* arg0 文件路径
* arg1 需要替换的内容
* arg2 新内容
*/
iteratorDirectory("d:/test","","");
}
public static void iteratorDirectory(String filepath,String oldStr,String replaceStr) {
File file = new File(filepath);
if (file.isDirectory()) {
String[] fileList = file.list();
for(int i=0;i<fileList.length;i++) {
iteratorDirectory(filepath+"\\"+fileList[i],oldStr,replaceStr);
}
}else {
replaceTxtByStr(filepath,oldStr,replaceStr);
}
}
public static void replaceTxtByStr(String path,String oldStr,String replaceStr) {
String temp = "";
int len = oldStr.length();
StringBuffer tempBuf = new StringBuffer();
try {
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
while((temp = br.readLine()) != null) {
if(temp.contains(oldStr)) {
int index = temp.indexOf(oldStr);
tempBuf.append(temp);
tempBuf.replace(index, index+len, replaceStr);
buf.append(tempBuf);
tempBuf.setLength(0);
}else {
buf.append(temp);
}
buf = buf.append(System.getProperty("line.separator"));
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 一个替换某文件夹下的所有文件中特定字符串的小工具
- 替换字符串在一个文件夹中所有的文件
- 小工具:批量转换文件夹下的特定类…
- Linux环境下替换工程文件的小工具及如何替换文件中的字符串
- 将一个目录下所有指定文件里面的特定字符串替换
- 将一个目录下所有指定文件里面的特定字符串替换
- linux下替换文件夹下的所有文件的字符串
- c++中 打开一个文件夹下的所有特定格式的文件(如JPG)
- 批量替换文件夹下所有文件的某个字符串
- Linux下的一个文件查询小工具
- Unity中获取特定文件夹下文件名包含特定字符串的文件路径
- 利用OPENCV写的对文件夹下所有图片大小归一化的小工具
- 一个html截取字符串的小工具
- 替换文件夹下包含字符串的文件
- 在一个文件夹下面的所有文件中查找字符串
- java读取文件夹下所有文件并替换文件每一行中指定的字符串
- 查找文件夹下的所有文件内容中的特定单词
- 一个文件修改监视的小工具
- sqldeveloper 安装
- php +mysql +apache 整合过程
- apt-get无法找到安装包解决方法--添加源
- Please ensure that adb is correctly located 问题的解决
- const map的初始化及使用
- 一个替换某文件夹下的所有文件中特定字符串的小工具
- 观后感---Jdk的安装和java的核心机制
- 使用logback 替换log4j实例
- 用C语言模拟面向对象
- ETL入门基础与设计讲解
- 一条shell指令统计项目的代码行数
- 华为C8815 默认logcat信息
- LeetCode | 3Sum
- Qt中处理First chance exception异常