Java之扫描目录,修改文件内容
扫描目录下文件,修改文件中指定内容
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之扫描目录,修改文件内容的更多相关文章
- Java之修改文件内容:字符串逐行替换
依赖包: <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</a ...
- java修改文件内容
文件的读和写,大家都不陌生,但是修改呢?按照普通的读写流去修改的话,只能全部读取出来,在内存中修改好后,全部写进去,这样对于文件内容过多的时,性能很低. 最近在遇到这个问题的时候,发现RandomAc ...
- Web 在线文件管理器学习笔记与总结(5)修改文件内容
① 读出要修改的文件的内容 ② 进行修改 ③ 将修改后的内容写进文件 index.php: <?php require 'dir.func.php'; require 'file.func.ph ...
- python笔记(三)---文件读写、修改文件内容、处理json、函数
文件读写(一) #r 只读,打开文件不存在的话,会报错 #w 只写,会清空原来文件的内容 #a 追加写,不会请求,打开的文件不存在的话,也会帮你新建的一个文件 print(f.read()) #获取到 ...
- linux下C++修改文件内容
C fwrite在任意位置写入文件,并可修改文件内容 想实现类似迅雷那样下载时可以从文件半中间写入的功能 #include<stdio.h> int main() { FILE *fp; ...
- python 修改文件内容
python 修改文件内容 一.修改原文件方式 1 def alter(file,old_str,new_str): 2 """ 3 替换文件中的字符串 4 :param ...
- python 文件操作(二) 替换性修改文件内容
正常情况我们想要仅对文件某一行的内容进行修改,而不改变其他内容,在原文件的基础上不能修改,因为当我们对原文件进行写操作时,如果原文件里面有内容,就会清空,在这种情况下,只能对文件进行替换性修改:即重新 ...
- Python修改文件内容
工作中要写个脚本来修改文件的内容,然后就写了一个刷子: #coding:utf8 import os def modify_file(old_file, new_version, old_versio ...
- shell编程系列12--文本处理三剑客之sed利用sed修改文件内容
shell编程系列12--文本处理三剑客之sed利用sed修改文件内容 修改命令对照表 编辑命令 1s/old/new/ 替换第1行内容old为new ,10s/old/new/ 替换第1行到10行的 ...
随机推荐
- Python2.7-marshal
marshal模块,和 pickle 模块功能基本相同,也是序列化数据,只不过 marshal 都序列化成二进制数据,由于没有官方统一,不同版本 marshal 的结果也会不一样,所以不推荐使用.ma ...
- Android 给CheckBox设置背景
一般来说我们给控件(Button,LinearLayout,ImageView,TextView等)设这背景的时候只需要设置这些控件的android:background即可, 但是在给CheckBo ...
- calico 排错记录 apt-get install telnet
1.用kubespray部署一个单节点集群,kubectl get pods -n kube-system,结果: calico-node-7v8wx 1/1 Running 0 2dcalico-n ...
- (四)Lua脚本语言入门(数组遍历)
这篇文章就当成铺垫型的文章,写着写着发现有好多想写的,,关于C#与Java,当然作为铺垫肯定与Lua的下部分介绍有关..... 对于"泛型",先看C#中"泛型" ...
- odoo字段
OpenERP对象字段定义的详解 4 OpenERP对象支持的字段类型有, 基础类型:char, text, boolean, integer, float, date, time, datetime ...
- Ubuntu系统上All-in-one部署OpenStack
虚拟机软件:VMware Workstaion12 操作系统:Ubuntu14.04 1.修改Ubuntu14.04的apt源为国内的阿里源: cp /etc/apt/sources.list /et ...
- iOS Swift WisdomScanKit二维码扫码SDK,自定义全屏拍照SDK,系统相册图片浏览,编辑SDK
iOS Swift WisdomScanKit 是一款强大的集二维码扫码,自定义全屏拍照,系统相册图片编辑多选和系统相册图片浏览功能于一身的 Framework SDK [1]前言: 今天给大家 ...
- 使用FindAncestor查找方式绑定且不需要使用datacontext
原文:使用FindAncestor查找方式绑定且不需要使用datacontext <UserControl x:Class="QuJiao.AnimationVideoPlayer&q ...
- python 相对路径导入 与 绝对路径导入
我的理解: 假设有一个文件夹 app 若 app 下有app/__init__.py文件,则此 app 被视作一个 package,而 app 下的其他文件/文件夹被视作 module 我们知道,pa ...
- jqGrid 列内容超过一定长度省略表示
jqgrid初始化方法中的,对应列添加formatter方法 colNames : [ "描述" ], colModel : [ { name : 'description', i ...