【Java】file操作-删除文件中某一行中符合某一规则的
2023-04-18 12:36:49 时间
效果
此处规则,删除已空格分隔的域名行,为防止因制表符等引起误删,强制插入的规则空格分隔
同时要过滤掉# 和其他非自己插入的数据格式,避免误删
代码
package com.ths.arsenaldnsnginxconfig.test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class FileUtil {
public void removeLineFromFile(String file, String lineToRemove) {
try {
File inFile = new File(file);
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
//Read from the original file and write to the new
//unless content matches data to be removed.
// 考虑注解行 跳过 ,正常行 空格长度不一致正则尝试
while ((line = br.readLine()) != null) {
if (!findRealmName(line, lineToRemove)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 查找完全匹配的域名
*/
private boolean findRealmName(String line, String s){
System.out.println("当前行情况"+line);
if (line.contains(" ")) {
System.out.println("当前行有制表符");
}
if (line.contains("#") || line.trim().isEmpty() || !line.contains(" ") || line.contains(" "))
return false;
String[] sArr = line.trim().replaceAll(" +", " ").split(" ");
System.out.println("切割到的空格后字符串"+ sArr[1]);
String realmName = sArr[1].substring(0, sArr[1].lastIndexOf('.'));
System.out.println("匹配到的字符串" + realmName);
if (s.equals(realmName))
return true;
return false;
}
/* public static void main(String[] args) {
FileUtil util = new FileUtil();
util.removeLineFromFile("D:/about/dns/test.txt", "hub.cn");
}
*/
public static void main(String[] args) {
// 考虑删除此类异常情况多空格,空格数量不定
String str = " abc def ghi mnopq r s ";
// 法1
// StringTokenizer pas = new StringTokenizer(str, " ");
// str = ""; //这里清空了str,但StringTokenizer对象中已经保留了原来字符串的内容。
//
// while (pas.hasMoreTokens()) {
// String s = pas.nextToken();
// str = str + s + " ";
// }
// System.out.println(str.trim());
// 法2
String s = str.trim().replaceAll(" +", " ");
System.out.println(s);
}
}
相关文章
- 【技术种草】cdn+轻量服务器+hugo=让博客“云原生”一下
- CLB运维&运营最佳实践 ---访问日志大洞察
- vnc方式登陆服务器
- 轻松学排序算法:眼睛直观感受几种常用排序算法
- 十二个经典的大数据项目
- 为什么使用 CDN 内容分发网络?
- 大数据——大数据默认端口号列表
- Weld 1.1.5.Final,JSR-299 的框架
- JavaFX 2012:彻底开源
- 提升as3程序性能的十大要点
- 通过凸面几何学进行独立于边际的在线多类学习
- 利用行动影响的规律性和部分已知的模型进行离线强化学习
- ModelLight:基于模型的交通信号控制的元强化学习
- 浅谈Visual Source Safe项目分支
- 基于先验知识的递归卡尔曼滤波的代理人联合状态和输入估计
- 结合网络结构和非线性恢复来提高声誉评估的性能
- 最佳实践丨云开发CloudBase多环境管理实践
- TimeVAE:用于生成多变量时间序列的变异自动编码器
- 具有线性阈值激活的神经网络:结构和算法
- 内网渗透之横向移动 -- 从域外向域内进行密码喷洒攻击