62,623
社区成员
发帖
与我相关
我的任务
分享import java.io.*;
class SrengHelper {
private String TEMP = "***";
private String[] oldFile;
private String[] newFile;
private String OLDSREngLOG = "D:/oldSREngLOG.log"; // 备份文件的路径;
private String NEWSREngLOG = "D:/newSREngLOG.log"; // 新文件的路径;
private String RESULTSREngLOG = "D:/resultFile.log"; // 输出文件的路径;
//------------------------------
// replaceLine Method
public SrengHelper replaceLine(String[] oldFile, String[] newFile) { // 选择法比较备份文件和新文件;
for(int i = 0; i < oldFile.length; i++) {
for(int j = 0; j < newFile.length; j++) {
if(oldFile[i].equals(newFile[j])) {
newFile[j] = newFile[j].replace(newFile[j], TEMP);
}
}
}
return this;
}
//------------------------------
// Output Method
public void Output(String[] newFile) { // 输出备份文件与新文件不同的行;
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(RESULTSREngLOG));
String s = null;
for(int i = 0; i < newFile.length; i++) {
if(!(newFile[i].equals(TEMP))) {
s = i + "." + newFile[i];
System.out.println(s);
bw.write(s);
bw.newLine();
}
}
bw.flush();
bw.close();
}catch(FileNotFoundException e) {
System.out.println("文件没有找到!");
System.exit(-1);
}catch(IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}
}
//------------------------------
// count oldFile
public int oldFileCount() { // 统计备份文件行数;
int oldFileCount = 0;
BufferedReader o = null;
try {
o = new BufferedReader(new FileReader(OLDSREngLOG));
while((o.readLine()) != null) {
oldFileCount ++;
}
}catch(FileNotFoundException e) {
System.out.println("文件没有找到!");
System.exit(-1);
}catch(IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}finally {
try {
o.close();
} catch (IOException e) {
System.out.println("文件关闭错误!");
System.exit(-1);
}
}
return oldFileCount;
}
//------------------------------
// count newFile
public int newFileCount() { // 统计新文件行数
int newFileCount = 0;
BufferedReader n = null;
try {
n = new BufferedReader(new FileReader(NEWSREngLOG));
while((n.readLine()) != null) {
newFileCount ++;
}
}catch(FileNotFoundException e) {
System.out.println("文件没有找到!");
System.exit(-1);
}catch(IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}finally {
try {
n.close();
} catch (IOException e) {
System.out.println("文件关闭错误!");
System.exit(-1);
}
}
return newFileCount;
}
//------------------------------
// save oldFile
public SrengHelper saveOldFile(String[] oldFile) { // 把备份文件保存到String[]数组;
BufferedReader br = null;
String s = null;
try {
br = new BufferedReader(new FileReader(OLDSREngLOG));
for(int i = 0; (s = br.readLine()) != null; i++) {
oldFile[i] = s;
}
}catch(FileNotFoundException e) {
System.out.println("文件没有找到!");
System.exit(-1);
}catch(IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}finally {
try {
br.close();
}catch (IOException e) {
System.out.println("文件关闭错误!");
System.exit(-1);
}
}
return this;
}
//------------------------------
// save newFile
public SrengHelper saveNewFile(String[] newFile) { // 把新文件保存到String[]数组;
BufferedReader bs = null;
String s = null;
try {
bs = new BufferedReader(new FileReader(NEWSREngLOG));
for(int i = 0; (s = bs.readLine()) != null; i++) {
newFile[i] = s;
}
}catch(FileNotFoundException e) {
System.out.println("文件没有找到!");
System.exit(-1);
}catch(IOException e) {
System.out.println("文件读取错误!");
System.exit(-1);
}finally {
try {
bs.close();
}catch (IOException e) {
System.out.println("文件关闭错误!");
System.exit(-1);
}
}
return this;
}
}
//------------------------------------------------------------
// Main Method
public class TestString {
public static void main(String[] args) {
SrengHelper sh = new SrengHelper();
String[] oldFile= new String[sh.oldFileCount()];
String[] newFile= new String[sh.newFileCount()];
sh.saveOldFile(oldFile).saveNewFile(newFile);
sh.replaceLine(oldFile, newFile).Output(newFile);
}
}
public static void main(String[] args) throws IOException {
File file = new File("d:/55.txt");
File file2 = new File("d:/55.txt.tmp");
BufferedReader reader = new BufferedReader(new FileReader(file));
PrintWriter writer = new PrintWriter(file2);
String line;
while ((line = reader.readLine()) != null) {
// 判断条件,根据自己的情况书写,会删除所有符合条件的行
if (line.startsWith("1899-12-30") && line.indexOf("0.000000") != -1) {
// 读取后面的几行,废弃
// reader.readLine();
// reader.readLine();
// reader.readLine();
continue;
}
writer.println(line);
writer.flush();
}
reader.close();
writer.close();
// 删除老文件
file.delete();
file2.renameTo(file);
}