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"); } }
相关文章推荐
- Java写入txt文件内容
- JAVA实现读取txt文件内容的方法
- java读取txt文件内容
- java实现list清除重复的字符串;arraylist动态数组;角谷定理最终得1,找出10000以内哪个数字的步骤最长
- java小知识-将System.out.println到控制台的内容打印并保存在新的文件中(例如txt文件从)
- java中读取txt文本内容
- Java使用Ajax读取txt记事本的内容
- java读取txt文件内容
- JAVA提取Word,Excel,PPT,PDF,TXT等文档文字内容
- java实现持续写入内容到txt文件
- 如何用java读取一个txt 文件内的内容并把它赋值与String里?
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java 如何将两个txt文件内容写入到一个txt文件中?
- java实现持续写入内容到txt文件
- Java读取文件夹下多个txt文本内容
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- java以行读取txt文本内容
- 【转】JAVA输出内容打印到TXT以及不同系统中如何换行
- Java读取txt文件中内容作为测试用例中的参数
- java 将内容写入文件 txt