您的位置:首页 > 编程语言 > Java开发

JAVA-清除txt中重复内容

2010-05-23 10:57 330 查看
该程序将一批txt文件分别清除重复内容存入一个新的txt文件中。具体思路是,首先对每个txt文件清除重复内容,分别写入temp.txt中转文件中,最后对temp.txt文件清除重复内容,再写入目标txt文件,并删除中转temp.txt文件。
package com;
import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
public class ResortByDelRed {
public void ResortToTemp(File oldFolder,String tempPath)throws IOException{
//String oldPath = oldFolderPath;
String str = null;
String string = null;
Vector vector = new Vector();////定义容器类对象
boolean IsRepeat = false;
try{
BufferedReader reader = new BufferedReader(new FileReader(oldFolder));
while((str=reader.readLine())!= null){
for(int i=0;i<vector.size();i++){
string = (String)vector.elementAt(i);
if(string.equals(str.trim())){//去掉前后空格后比较
IsRepeat = true;
break;
}
}
if(IsRepeat){
IsRepeat = false;
}
else{
vector.add(str.trim());
}
}
reader.close();
}catch(IOException e)
{
e.printStackTrace();
}
//创建临时中转txt文件
String newFile = tempPath;
File tempFile = new File(newFile);
if(!tempFile.exists()){
tempFile.createNewFile();
}
try{
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile,true));
for(int i=0;i<vector.size();i++){
str = (String)vector.elementAt(i);
writer.write(str);
writer.newLine();
}
writer.close();
}catch(IOException e){
e.printStackTrace();
}
}
public void AllResortToTemp(String oldFolderPath,String tempPath)throws IOException{
File file = new File(oldFolderPath);
File[] childs = file.listFiles();
for(int i=0;i<childs.length;i++){
ResortToTemp(childs[i],tempPath);
}
}
public void DelTemp(String tempPath)throws IOException{
try{
File tempFile = new File(tempPath);
if(!tempFile.exists()){
JOptionPane.showMessageDialog(null, "没有生成temp.txt文件");
return;
}

tempFile.delete();
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args)throws IOException{
String tempPath = "E:/temp.txt";
String sourceFolder = "E:/c";
String resultPath = "E:/result.txt";
File tempFile = new File(tempPath);
ResortByDelRed resort = new ResortByDelRed();
resort.AllResortToTemp(sourceFolder,tempPath);//首先放到temp.txt,然后删除
resort.ResortToTemp(tempFile,resultPath);//把temp文件再去重放入result文件
resort.DelTemp(tempPath);//删除temp.txt中转文件
System.out.println("OK");

}
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息