扫描目录下文件,修改文件中指定内容

 package org.utils.tools.fileoper;

 import java.io.*;
import java.util.ArrayList;
import java.util.List; /*
* 修改文件中的内容
* 替换properties文件中的ip
* */
public class EditFile {
public static void main(String args[]) {
// String inputPath = "C:\\workspace\\hbase_test\\src\\main\\resource\\properties\\case_01.properties";
// String outputPath = "C:\\workspace\\hbase_test\\src\\main\\resource\\properties\\case_out.properties"; String srcStr = "bd.test.com"; //需要替换的字符串
String desStr = "10.15.100.25"; //用于替换的字符串
// 文件目录,不扫子目录
String dirPath = "C:\\workspace\\work\\bdd-bd-test\\" +
"src\\test\\resources\\properties\\case\\tunny\\001"; File f = new File(dirPath);
String wholeFilePath;
String[] fileNames = f.list();
for (String s : fileNames) {
wholeFilePath = dirPath + "\\" + s;
System.out.println("处理文件:" + wholeFilePath);
propertiesChange(wholeFilePath, srcStr, desStr);
}
} /*
* 修改文件中的指定内容
* */
public static void propertiesChange(String filePath, String srcStr, String desStr) {
//字符流
FileReader fr = null;
FileWriter fw = null;
//缓冲流
BufferedReader br = null;
BufferedWriter bw = null; List list = new ArrayList<>();
//读取文件内容保证在list中
try {
fr = new FileReader(new File(filePath));
br = new BufferedReader(fr); //扩容,类似加水管
String line = br.readLine(); //逐行复制
while (line != null) {
//修改指定内容
if (line.contains(srcStr)) {
line = line.replace(srcStr, desStr);
}
list.add(line);
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭流,顺序与打开相反
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
} //将list中内容输出到原文件中
try {
fw = new FileWriter(filePath);
bw = new BufferedWriter(fw);
for (Object s : list) {
bw.write((String) s);
bw.newLine(); //换行输出
}
System.out.println("文件修改成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭流,顺序与打开相反
bw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /*
* 读取文件并修改指定内容,复制到另一个文件中
* */
public static void propertiesChange(String inputPath, String outputPath, String srcStr, String desStr) {
//字符流
FileReader fr = null;
FileWriter fw = null;
//缓冲流
BufferedReader br = null;
BufferedWriter bw = null; try {
fr = new FileReader(new File(inputPath));
br = new BufferedReader(fr); //扩容,类似加水管
fw = new FileWriter(outputPath);
bw = new BufferedWriter(fw); String line = br.readLine(); //逐行复制
while (line != null) {
if (line.contains(srcStr)) {
line = line.replace(srcStr, desStr);
}
bw.write(line);
bw.newLine(); //换行输出
line = br.readLine();
}
System.out.println("文件修改成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭流,顺序与打开相反
bw.close();
br.close();
fw.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} }

Java之扫描目录,修改文件内容的更多相关文章

  1. Java之修改文件内容:字符串逐行替换

    依赖包: <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</a ...

  2. java修改文件内容

    文件的读和写,大家都不陌生,但是修改呢?按照普通的读写流去修改的话,只能全部读取出来,在内存中修改好后,全部写进去,这样对于文件内容过多的时,性能很低. 最近在遇到这个问题的时候,发现RandomAc ...

  3. Web 在线文件管理器学习笔记与总结(5)修改文件内容

    ① 读出要修改的文件的内容 ② 进行修改 ③ 将修改后的内容写进文件 index.php: <?php require 'dir.func.php'; require 'file.func.ph ...

  4. python笔记(三)---文件读写、修改文件内容、处理json、函数

    文件读写(一) #r 只读,打开文件不存在的话,会报错 #w 只写,会清空原来文件的内容 #a 追加写,不会请求,打开的文件不存在的话,也会帮你新建的一个文件 print(f.read()) #获取到 ...

  5. linux下C++修改文件内容

    C fwrite在任意位置写入文件,并可修改文件内容 想实现类似迅雷那样下载时可以从文件半中间写入的功能 #include<stdio.h> int main() { FILE *fp; ...

  6. python 修改文件内容

    python 修改文件内容 一.修改原文件方式 1 def alter(file,old_str,new_str): 2 """ 3 替换文件中的字符串 4 :param ...

  7. python 文件操作(二) 替换性修改文件内容

    正常情况我们想要仅对文件某一行的内容进行修改,而不改变其他内容,在原文件的基础上不能修改,因为当我们对原文件进行写操作时,如果原文件里面有内容,就会清空,在这种情况下,只能对文件进行替换性修改:即重新 ...

  8. Python修改文件内容

    工作中要写个脚本来修改文件的内容,然后就写了一个刷子: #coding:utf8 import os def modify_file(old_file, new_version, old_versio ...

  9. shell编程系列12--文本处理三剑客之sed利用sed修改文件内容

    shell编程系列12--文本处理三剑客之sed利用sed修改文件内容 修改命令对照表 编辑命令 1s/old/new/ 替换第1行内容old为new ,10s/old/new/ 替换第1行到10行的 ...

随机推荐

  1. Android检查手机上是否安装了第三方软件的方法------本文以百度地图为例

    package com.example.myapi.thirdbaidumap; import java.net.URISyntaxException; import java.util.ArrayL ...

  2. odoo之显示前端,数据,可选择

    def create(self,cr,uid,vals,context=None): if context is None: context ={} if vals.get('name','/')== ...

  3. WCF来传递DataTable的Bug

    Wcf,客户端与服务器之间在传递DataTable(由于数据库字段不确定暂时用DataTable而不是用实体对象传递)时,发现有的DataTable可以直接传递没有问题 解决方案: DataTable ...

  4. 【小程序】访问 https配置的数据接口

    小程序对于网络请求的URL的特殊要求:1)不能出现端口号;    2)不能用localhost;       3)  必须用https (一)搭建本地https服务器(windows) 搭建出来的服务 ...

  5. 20155310 《网络攻防》Exp4 恶意代码分析

    20155310 <网络攻防>Exp4 恶意代码分析 基础问题 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些, ...

  6. C# event线程安全

    突然想到有关C#中使用event特性时关于线程安全的问题,以前虽然有遵从“复制引用+null判断”的模式(盲目地),但没有深入了解和思考. 为之查询了资料和实验,对此有了进一步的理解. 一般event ...

  7. 【第三课】Centos 7.x系统安装和网络配置以及远程密钥登录

    目录 一.安装CentOS 7.3 二.配置网络 1.使用dhclient命令自动获取ip地址 2.使用ip addr或ifconfig命令查看网卡信息 3.使用route命令查看路由信息 4.通过修 ...

  8. centos7 部署 nginx+tomcat+MariaDB 环境并安装安全狗,使用natapp隧道

    jdk安装: -openjdk 参考:https://blog.csdn.net/dhr201499/article/details/81626466 tomcat安装: 使用版本:8.5.37 参考 ...

  9. 【ORACLE】碎片整理

    alter table test enable row movement; alter table test shrink space; execute dbms_stats.gather_table ...

  10. vue初学实践之路——vue简单日历组件(3)

    这一篇我们来实现管理员修改每一天剩余数量的功能. <div id="calendar"> <div id="left"> <spa ...