import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter; public class ChangeFile {
public static void main(String[] args) {
try {
BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:/EntNatureDistributionDAO.txt"))));//数据流读取文件 StringBuffer strBuffer = new StringBuffer();
String empty = "";
String tihuan = "";
for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {
if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判断当前行是否存在想要替换掉的字符 -1表示存在
tihuan = temp.substring(0, 10);
temp = temp.replace(tihuan, empty);//替换为你想要的东东
}
strBuffer.append(temp);
strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割
}
bufReader.close();
PrintWriter printWriter = new PrintWriter("E:/EntNatureDistributionDAO.txt");//替换后输出的文件位置
printWriter.write(strBuffer.toString().toCharArray());
printWriter.flush();
printWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} 适用:如服务器崩溃 导致文件丢失,还原后类文件在每一行的开头都加了很多注释(如下)
/* */ package com.itown.iesap.starquery.dao;
/* */
/* */ import com.itown.framework.impl.ThreadContext;
/* */ import com.itown.framework.persistence.AbstractDao;
.........很多很多..... 替换之后就是这样的:
package com.itown.iesap.starquery.dao; import com.itown.framework.impl.ThreadContext;
import com.itown.framework.persistence.AbstractDao;
.........很多很多...... 如果你又成百上千个这样的文件替换那就要读取文件夹下的所有文件:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter; public class ChangeFile {
public static void main(String[] args) {
try {
//读取指定文件夹下的所有文件
String filepath = "D:/AOE/abc/";//给我你的目录文件夹路径
File file = new File(filepath);
if (!file.isDirectory()) {
System.out.println("---------- 该文件不是一个目录文件 ----------");
} else if (file.isDirectory()) {
System.out.println("---------- 很好,这是一个目录文件夹 ----------");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filepath + "\\" + filelist[i]);
//String path = readfile.getPath();//文件路径
String absolutepath = readfile.getAbsolutePath();//文件的绝对路径
String filename = readfile.getName();//读到的文件名
//////// 开始挨个的读取文件 ////////
BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(absolutepath)));//数据流读取文件
StringBuffer strBuffer = new StringBuffer();
String empty = "";
String tihuan = "";
for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {
if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判断当前行是否存在想要替换掉的字符 -1表示存在
tihuan = temp.substring(0, 9);//这里截取多长自己改
temp = temp.replace(tihuan, empty);//替换为你想要的东东
}
strBuffer.append(temp);
strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割
}
bufReader.close();
PrintWriter printWriter = new PrintWriter("E:/ttt/"+filename);//替换后输出的文件位置(切记这里的E:/ttt 在你的本地必须有这个文件夹)
printWriter.write(strBuffer.toString().toCharArray());
printWriter.flush();
printWriter.close();
System.out.println("ok 第 " + (i+1) +" 个文件操作成功!");
//////// 读取兵输出一个文件结束 ////////
}
System.out.println("---------- 所有文件操作完毕 ----------");
}
} catch (Exception e) {
e.printStackTrace();
}
}
} 这样更加清晰明了些:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter; public class ReadFile { /**
* 主方法测试
* @param args
* @author 杜文俊
* @update 2013-6-26 下午1:36:31
*/
public static void main(String[] args) {
String filePath = "D:/AOE/abc/"; //给我你要读取的文件夹路径
File outPath = new File("E:/AOE/abc/"); //随便给一个输出文件夹的路径(不存在也可以)
readFolder(filePath,outPath);
} /**
* 读取文件夹
* @return
*/
public static void readFolder(String filePath,File outPath){
try {
//读取指定文件夹下的所有文件
File file = new File(filePath);
if (!file.isDirectory()) {
System.out.println("---------- 该文件不是一个目录文件 ----------");
} else if (file.isDirectory()) {
System.out.println("---------- 很好,这是一个目录文件夹 ----------");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(filePath + "\\" + filelist[i]);
//String path = readfile.getPath();//文件路径
String absolutepath = readfile.getAbsolutePath();//文件的绝对路径
String filename = readfile.getName();//读到的文件名
readFile(absolutepath,filename,i,outPath);//调用 readFile 方法读取文件夹下所有文件
}
System.out.println("---------- 所有文件操作完毕 ----------");
}
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 读取文件夹下的文件
* @return
*/
public static void readFile(String absolutepath,String filename,int index,File outPath){
try{
BufferedReader bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(absolutepath)));//数据流读取文件
StringBuffer strBuffer = new StringBuffer();
String empty = "";
String tihuan = "";
for (String temp = null; (temp = bufReader.readLine()) != null; temp = null) {
if(temp.indexOf("/*") != -1 && temp.indexOf("*/") != -1){ //判断当前行是否存在想要替换掉的字符 -1表示存在
tihuan = temp.substring(0, 9);//这里截取多长自己改
temp = temp.replace(tihuan, empty);//替换为你想要的东东
}
strBuffer.append(temp);
strBuffer.append(System.getProperty("line.separator"));//行与行之间的分割
}
bufReader.close();
if(outPath.exists() == false){ //检查输出文件夹是否存在,若不存在先创建
outPath.mkdirs();
System.out.println("已成功创建输出文件夹:" + outPath);
}
PrintWriter printWriter = new PrintWriter(outPath+"\\"+filename);//替换后输出的文件位置(切记这里的E:/ttt 在你的本地必须有这个文件夹)
printWriter.write(strBuffer.toString().toCharArray());
printWriter.flush();
printWriter.close();
System.out.println("第 " + (index+1) +" 个文件 "+ absolutepath +" 已成功输出到 " +outPath+"\\"+filename);
}catch(Exception e){
e.printStackTrace();
}
}
}

java读取文件夹下所有文件并替换文件每一行中指定的字符串的更多相关文章

  1. java实现批量修改指定文件夹下所有后缀名的文件为另外后缀名的代码

    java实现批量修改指定文件夹下所有后缀名的文件为另外后缀名的代码 作者:Vashon package com.ywx.batchrename; import java.io.File; import ...

  2. 怎么统计指定文件夹下含有.xml格式的文件数目

    如何统计指定文件夹下含有.xml格式的文件数目?如题 ------解决思路----------------------Directory.GetFiles(@"路径", " ...

  3. Python遍历一个文件夹下有几个Excel文件及每个Excel文件有几个Sheet

    一. 解决问题: 工作中常会遇到合并Excel文件的需求,Excel文件数量不确定,里面的Sheet 数量是可变的,Sheet Name是可变的,所以,需要用到遍历一个文件夹下有几个Excel文件,判 ...

  4. Java获取Linux上指定文件夹下所有第一级子文件夹

    说明:需要只获得第一级文件夹目录 package com.sunsheen.jfids.studio.monitor.utils; import java.io.BufferedReader; imp ...

  5. 使用 OLEDB 及 SqlBulkCopy 将多个不在同一文件夹下的 ACCESS mdb 数据文件导入MSSQL

    注:转载请标明文章原始出处及作者信息http://www.cnblogs.com/z-huifei/p/7380388.html 前言 OLE DB 是微软的战略性的通向不同的数据源的低级应用程序接口 ...

  6. Windows操作系统单文件夹下到底能存放多少文件及单文件的最大容量

    本文是转自:http://hi.baidu.com/aqgjoypubihoqxr/item/c896921f8c2eaba5feded5f2         最近需要了解Windows中单个文件夹下 ...

  7. ubuntu18.04 复制或剪切某文件夹下的前x个文件到另一个文件夹下

    该代码可以将file_path_src文件夹中的前cnt个文件,剪切或复制到file_path_tar文件夹下,前提是file_path_src中的文件名可以排序.如VOC数据集提取某个类的图片和xm ...

  8. tomcat的bin文件夹下的.bat和.sh文件

    tomcat的bin文件夹中存在一份.bat文件和相对应的.sh文件,一个是为了在window系统上执行的文件,另一个是linux下的批处理文件.例如:startup.bat和startup.sh. ...

  9. linux命令(13) 删除指定文件夹下后缀名相同的文件

    方法一: find 目录 -name "*.abc" | xargs rm命令有点危险,可以先执行前半段,看看是不是你要删除的文件, 然后再整条执行 方法二:find . -nam ...

随机推荐

  1. linux-网卡故障

    Linux 网络问题解决思路 1.查看 /etc/sysconfig/network-script/ 查看eth0和eth1的配置是否正确 2.查看 /etc/modual.conf 的配置模块是否正 ...

  2. Codeforces 417E

    #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #inclu ...

  3. NSString length的坑。

    说坑,可能过头了,是我理所当然的把OC看作C了, char* cstr = "zh中文12"; NSString* s = [NSString stringWithUTF8Stri ...

  4. display:flex

    元素在x方向走,元素y不一样[高度].可以用对齐.align-items. align-self 自身调节元素在x方向走,元素在x方向距离.justify-content .   元素在x方向走,x方 ...

  5. lucene4.7学习总结

    花了一段时间学习lucene今天有时间把所学的写下来,网上有很多文章但大部分都是2.X和3.X版本的(当前最新版本4.9),希望这篇文章对自己和初学者有所帮助. 学习目录 (1)什么是lucene ( ...

  6. 使用eclipse创建在myeclipse中运行的web工程

    今天在跟随慕课网学习java时,遇到课程中老师使用Myeclipse,我用的是eclipse,那么就使用eclipse创建在Myeclipse项目 参考: 如何在Eclipse配置Tomcat服务器 ...

  7. Struts2标签<s:textfield/>

    中的name="",取值顺序是,如不加#,先从root中栈顶取,没有取到(即使栈顶有这个属性但是属性为空还是向下取,这点和el表达式不同)就去actioncontext的map中取 ...

  8. c++的调试与运行

    编译F9:运行F10:编译运行F11. 设置断点:在代码所在行的行首单击,该行即被加亮.注意:设置断点后,此时程序运行进入调试状态,要想运行程序,就不能使用F10或者F11,而是要使用F5调试,然后使 ...

  9. 小米Recovery线刷精灵 v1.0.0 破解版

    下载地址:http://www.crsky.com/soft/75923.html 小米Recovery线刷精灵支持将Recovery线刷包一键刷入小米手机,支持小米所有型号. 小米Recovery线 ...

  10. WebGis应用开发框架

    转自:http://www.cnblogs.com/zitsing/archive/2012/03/02/2377083.html 前言 Web Gis顾名思义就是通过浏览器方式操作的地理系统.通过浏 ...