import org.junit.Test;

 import java.io.*;

 /**
* User: HYY
* Date: 13-8-18
* Time: 下午8:11
* To change this template use File | Settings | File Templates.
*/
public class ReplaceStr {
private File fileRootDir = new File("D:\\ahgw\\src\\com\\ahgw");//根目录文件
private String saveDirPath = "D:\\ahgw\\src\\com\\ahgw2";//替换之后的新的一个目录名称
private String srcStr = "baoxiu";//要替换的原字符串
private String destStr = "ahgw";//目的字符串 @Test
public void test() throws IOException { if (!fileRootDir.exists()) {
throw new ExceptionInInitializerError("根目录不存在");
} File saveRootDir = new File(saveDirPath);
if (!saveRootDir.exists()) {
saveRootDir.mkdirs();
} File[] files = fileRootDir.listFiles();
operate(files);
} public void operate(File[] files) throws IOException {
for (File file : files) {
if (file.isDirectory()) {
if (file.listFiles().length == 0) {
file.mkdir();
System.out.println(file.listFiles().length);
} else {
operate(file.listFiles());
}
} else {
System.out.println("srcPath=" + file.getPath());
System.out.println("fileRootDir.getPath()=" + fileRootDir.getPath()); String savePath = saveDirPath + file.getPath().substring(fileRootDir.getPath().length());
// String savePath = fileRootDir.getParent() + File.separator + saveDirName + File.separator + file.getName();
File saveFile = new File(savePath);
System.out.println("savePath=" + savePath);
replace(file, saveFile);
}
}
} /**
* 根据源文件,修改相应的字符串,并保存起来
*
* @param file 源文件
* @param saveFile 保存的目标文件
*/
public void replace(File file, File saveFile) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
PrintWriter pw = new PrintWriter(saveFile);
String line;
while ((line = br.readLine()) != null) {
line = line.replaceAll(srcStr, destStr);
pw.println(line);
}
br.close();
pw.close();
} public static void main(String[] args) throws IOException { }
}

Java将整个文件夹里的文本中的字符串替换成另外一个字符串(可用于项目复制,变成另一个项目)的更多相关文章

  1. 用java实现删除文件夹里的所有文件

    package com.org.improve.contact; import java.io.File; public class DeletePaper { /** * @param args * ...

  2. java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用

    java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用 import java.io.BufferedReader; import java.io.BufferedWrite ...

  3. Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  4. MyEclipse里项目部署到tomcat上之后,tomcat webpps文件夹里为什么找不到这个项目

         今天在MyEclipse中部署了一个java web项目,然后发现报404错误,跑到tomcat目录下的webapps文件夹里并发现没有这个项目,才发现MyEclipse没有写入webapp ...

  5. Java字节流实现文件夹的拷贝

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...

  6. (转)android res文件夹里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)

    android res文件夹里面的drawable(ldpi.mdpi.hdpi.xhdpi.xxhdpi) (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),F ...

  7. su认证失败&文件夹里打开终端的方法&atom安装

    很久没用笔记本上的ubuntu,用不顺手,比在公司调教了半年多的电脑差远了.一步一步来.先解决最不顺手的三件事 1.su认证失败. 新安装的ubuntu系统是无法切换到root账户的,得做一番修改 s ...

  8. JAVA文件夹导入到Eclipse中方法:

    将JAVA文件夹导入到Eclipse中方法:方法一: 直接将java文件夹复制,然后粘贴到项目下:方法二:1.打开eclipse,点击项目的空白处,选择import:2.选择Existing Proj ...

  9. SQL扫描并执行文件夹里的sql脚本

    场景:项目数据库操作全部使用存储过程实现.每天都会有很多存储过程更新/增加,人工对测试环境中存储过程更新,会有一定概率出现遗漏,也麻烦!所以,需要一个工具将文件夹中所有存         储过程执行一 ...

随机推荐

  1. c# DateTime时间格式和JAVA时间戳格式相互转换

    /// java时间戳格式时间戳转为C#格式时间 public static DateTime GetTime(long timeStamp) { DateTime dtStart = TimeZon ...

  2. 移动Web单行文字垂直居中的问题

    单行文字垂直居中的方式你可能可以脱口而出,height和line-height设置为同样就行了,或者设置相同的padding-top和padding-bottom值. 上图是Chrome浏览器下的效果 ...

  3. c#基础汇总-------------封装

    说到封装,其实是比较基础类的问题,它为程序设计提供了系统与系统,模块与模块,类与类之间交互的实现手段.在.Net中,一切看起来都已经被包装在.Net FrameWork这一复杂的网络中,提供给最终开发 ...

  4. $设置背景图片的css

    $('.d-game-pic').css('background-image', 'url(' + App.getImg(gameDetail.desc_pic) + ')');

  5. [译]servlet3.0与non-blocking服务端推送技术

    Non-blocking(NIO)Server Push and Servlet 3 在我的前一篇文章写道如何期待成熟的使用node.js.假定有一个框架,基于该框架,开发者只需要定义协议及相关的ha ...

  6. flex打印图片

    <?xml version="1.0" encoding="utf-8"?><s:WindowedApplication xmlns:fx=& ...

  7. c#教程之事件处理函数的参数

    事件处理函数一般有两个参数,第一个参数(object sender)为产生该事件的对象的属性Name的值,例如上例单击标题为红色的按钮,第一个参数sender的值为button1.如上例标题为红色的按 ...

  8. html template

    https://wrapbootstrap.com/tag/single-page http://themeforest.net/ https://wrapbootstrap.com/themes h ...

  9. crontab定时任务中文乱码问题

    手动执行都很正常的的脚本,添加到定时任务中日志文件全是乱码经过多方查证终于找到了原因! crontab启动的任务没有获取系统的环境变量,导致中文乱码解决办法:   在执行的脚步中添加编码方式或者添加对 ...

  10. Nhibernate 多对多级联删除

    在网上找到的方法:查看这里 //-------------------------------------Article.hbm.xml-------------------------------- ...