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. sql删除多余重复的数据只保留一条

    delete from people where   peopleName in (select peopleName    from people group by peopleName      ...

  2. "琳琅满屋"调查问卷 心得体会及结果分析

    ·关于心得体会       当时小组提出这个校园二手交易市场的时候,就确定了对象范围,仅仅是面向在校大学生,而且在我们之前就已经有了很多成功的商品交易的例子可以让我们去借鉴,再加上我们或多或少的有过网 ...

  3. 大学生成绩管理系统(C语言)

    功能:成绩管理系统包含了学生的全部信息,每个学生是一个记录,包括学号,姓名,性别,班级,各科成绩(语数外). 系统功能: 1.信息录入——录入学生信息: 2.信息输出——显示所有信息: 3.信息查询— ...

  4. CCNA 4.14 TP Correction

    All people seem to need data processing ( Application presentation session transport network data li ...

  5. [pjsip]Pjlib中的链表结构

    Pjlib的链表结构跟常见的链表结构有所区别,如下图所示: 图1:一般链表结构 图2:pjlib中的链表结构 可以看到一般的双向链表是链表节点包含数据域,而pjlib中是数据域包含链表节点.一般的链表 ...

  6. SVN 首次用TortoiseSVN Checkout 提示Unexpected HTTP status 405

    权限错误 首次使用 因为没有 弹出 用户名密码输入框 无法输入 帐户信息. 解决办法: 点击 仓库地址输入栏 右边 的...按钮 此时弹出的输入框浏览器方式访问的.输入用户名和密码,然后在左侧出来的仓 ...

  7. Tomcat的安装(一)

    一.Tomcat文件下载类型 1.tar.gz 文件是linux的安装包 2.exe文件是Windows系统的安装包 3.zip文件是Windows系统下压缩版(解压缩即可,免安装) 二.下面使用zi ...

  8. BroadcastReceiver的最简单用法

    android系统下的广播: 电池电量低.电池充电完成.短信到来了.程序安装卸载.sd卡卸载安装 等 BrocastReceiverDemo.java public class BrocastRece ...

  9. Ogre学习笔记Basic Tutorial 前四课总结

    转自:http://blog.csdn.net/yanonsoftware/article/details/1011195 OGRE Homepage:http://www.ogre3d.org/   ...

  10. hdoj-2025

    #include "stdio.h"#include "string.h"void sort(char ch[],int count[],int n,int f ...