java读取文件夹下所有文件并替换文件每一行中指定的字符串
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读取文件夹下所有文件并替换文件每一行中指定的字符串的更多相关文章
- java实现批量修改指定文件夹下所有后缀名的文件为另外后缀名的代码
java实现批量修改指定文件夹下所有后缀名的文件为另外后缀名的代码 作者:Vashon package com.ywx.batchrename; import java.io.File; import ...
- 怎么统计指定文件夹下含有.xml格式的文件数目
如何统计指定文件夹下含有.xml格式的文件数目?如题 ------解决思路----------------------Directory.GetFiles(@"路径", " ...
- Python遍历一个文件夹下有几个Excel文件及每个Excel文件有几个Sheet
一. 解决问题: 工作中常会遇到合并Excel文件的需求,Excel文件数量不确定,里面的Sheet 数量是可变的,Sheet Name是可变的,所以,需要用到遍历一个文件夹下有几个Excel文件,判 ...
- Java获取Linux上指定文件夹下所有第一级子文件夹
说明:需要只获得第一级文件夹目录 package com.sunsheen.jfids.studio.monitor.utils; import java.io.BufferedReader; imp ...
- 使用 OLEDB 及 SqlBulkCopy 将多个不在同一文件夹下的 ACCESS mdb 数据文件导入MSSQL
注:转载请标明文章原始出处及作者信息http://www.cnblogs.com/z-huifei/p/7380388.html 前言 OLE DB 是微软的战略性的通向不同的数据源的低级应用程序接口 ...
- Windows操作系统单文件夹下到底能存放多少文件及单文件的最大容量
本文是转自:http://hi.baidu.com/aqgjoypubihoqxr/item/c896921f8c2eaba5feded5f2 最近需要了解Windows中单个文件夹下 ...
- ubuntu18.04 复制或剪切某文件夹下的前x个文件到另一个文件夹下
该代码可以将file_path_src文件夹中的前cnt个文件,剪切或复制到file_path_tar文件夹下,前提是file_path_src中的文件名可以排序.如VOC数据集提取某个类的图片和xm ...
- tomcat的bin文件夹下的.bat和.sh文件
tomcat的bin文件夹中存在一份.bat文件和相对应的.sh文件,一个是为了在window系统上执行的文件,另一个是linux下的批处理文件.例如:startup.bat和startup.sh. ...
- linux命令(13) 删除指定文件夹下后缀名相同的文件
方法一: find 目录 -name "*.abc" | xargs rm命令有点危险,可以先执行前半段,看看是不是你要删除的文件, 然后再整条执行 方法二:find . -nam ...
随机推荐
- 使用Obsolete特性来标记方法过时或弃用
我们在维护一些老的系统的时候,经常会遇到某个方法不再使用的情况,我们又不能直接将其删除,因为系统中可能还有很多地方有引用它,所以比较安全保险的做法是,使用Obsolete特性来标记它过时或弃用.如下代 ...
- 关于HTML5应用开发功耗调优化小结
HTML5的优化一直是困扰我的难题,特别是在移动端开发游戏和应用,所以对此进行了一些总结: 功耗优化点介绍 在移动设备中主要的功耗点在: 1. 网络的传输, 不管是3G网络还是WiFi传输都是移动设备 ...
- tableView里删除单元格
tableView里删除单元格 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSInde ...
- SuperGridControl 使用小技巧
1.显示行号 superGridControl1.PrimaryGrid.ShowRowGridIndex = true; 2.允许调整行头的宽度 superGridControl1.PrimaryG ...
- Fragment在xml中但作用不是显示view
2013-12-17 有时候会发现在xml文件中有使用fragment,但是却不是为了显示View,代码如下: <FrameLayout xmlns:android="http://s ...
- C++-多重继承的注意点
1, 钻石型多重继承如果不想要底部的类有重复的变量,则需要声明为virtual继承 class File{...}; class InputFile: virtual public File{..}; ...
- c# Winforms WebBrowser - Clear all cookies
Hello, I recently search for a method to delete all cookies from the build in .NET WinForms WebBro ...
- jQuery 自定义扩展,与$冲突处理
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 北大poj-1001
Description Problems involving the computation of exact values of very large magnitude and precision ...
- PHP中的文件上传
文件上传: 1.单个文件上传 2.多个文件上传 一.PHP配置文件中和上传有关的选项 file_uploads=on upload_max_filesize= 最大 ...