Java将整个文件夹里的文本中的字符串替换成另外一个字符串(可用于项目复制,变成另一个项目)
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将整个文件夹里的文本中的字符串替换成另外一个字符串(可用于项目复制,变成另一个项目)的更多相关文章
- 用java实现删除文件夹里的所有文件
package com.org.improve.contact; import java.io.File; public class DeletePaper { /** * @param args * ...
- java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用
java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用 import java.io.BufferedReader; import java.io.BufferedWrite ...
- Java以流的方式将指定文件夹里的.txt文件全部复制到另一文件夹,并删除原文件夹中所有.txt文件
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...
- MyEclipse里项目部署到tomcat上之后,tomcat webpps文件夹里为什么找不到这个项目
今天在MyEclipse中部署了一个java web项目,然后发现报404错误,跑到tomcat目录下的webapps文件夹里并发现没有这个项目,才发现MyEclipse没有写入webapp ...
- Java字节流实现文件夹的拷贝
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...
- (转)android res文件夹里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)
android res文件夹里面的drawable(ldpi.mdpi.hdpi.xhdpi.xxhdpi) (1)drawable-hdpi里面存放高分辨率的图片,如WVGA (480x800),F ...
- su认证失败&文件夹里打开终端的方法&atom安装
很久没用笔记本上的ubuntu,用不顺手,比在公司调教了半年多的电脑差远了.一步一步来.先解决最不顺手的三件事 1.su认证失败. 新安装的ubuntu系统是无法切换到root账户的,得做一番修改 s ...
- JAVA文件夹导入到Eclipse中方法:
将JAVA文件夹导入到Eclipse中方法:方法一: 直接将java文件夹复制,然后粘贴到项目下:方法二:1.打开eclipse,点击项目的空白处,选择import:2.选择Existing Proj ...
- SQL扫描并执行文件夹里的sql脚本
场景:项目数据库操作全部使用存储过程实现.每天都会有很多存储过程更新/增加,人工对测试环境中存储过程更新,会有一定概率出现遗漏,也麻烦!所以,需要一个工具将文件夹中所有存 储过程执行一 ...
随机推荐
- 【poj4011】Automated Telephone Exchange
题目:Automated Telephone Exchange poj URL:http://poj.org/problem?id=4011 原题如下图: 题意: 就是一个三位数减去两个小于或等于99 ...
- PSP个人软件开发工具需求分析文档
第一部分:前景与范围 1.业务需求 1.1 背景 在目前的软件项目开发过程中,进度计划总是非常不准确,经常出现延期,而且大多数都无法给出一个相对比较准确的延迟时间.即使使用用例包.用例的方式组织需求, ...
- Swift属性
属性的存储 属性的主要作用是存储数据,可以常量属性和变量属 性: struct FixedLengthRange { var firstValue: Int let length: Int } var ...
- Quartz 第五课 SimpleTriggers 官方文档翻译
对于SimpleTrigger你需要知道它的启动总是在一个特殊的时间点或者有你设置的重复时间段中.直白来说,如果你想在2005年1月13日,正好上午11时23分54秒触发,然后执行五次,每十秒钟. 从 ...
- 控制GridView中字段的长度,规范数据
前台: <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridVi ...
- OC12_自动释放池
// // Dog.h // OC12_自动释放池 // // Created by zhangxueming on 15/6/18. // Copyright (c) 2015年 zhangxuem ...
- 笔试面试题-小米Git
题目描述: git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base'<--base<--A<--A' ^ | --- B<--B' 小米工程师常 ...
- What are Upgrade, Product and Package Codes used for? By pusu
Following content is reprinted from here, please go to the original website for more information. Au ...
- 【转】Qt使用自带的windeployqt 生成exe来发布软件
集成开发环境 QtCreator 目前生成图形界面程序 exe 大致可以分为两类:Qt Widgets Application 和 Qt Quick Application.下面分别介绍这两类exe ...
- dbt
Procedure Relocate(s : state; b : base_index) { Move base for state s to a new place beginning at b ...