两个txt去重java,java IO代码有关问题:将两个txt文件内容分别去重并去除重复内容后写入到另外两个txt文件,下面代码根本写不进(3)...

// boolean IsRepeat = false;

try {

BufferedReader reader1 = new BufferedReader(new FileReader(f1));

BufferedReader reader2 = new BufferedReader(new FileReader(f2));

while ((str1 = reader1.readLine()) != null

&& (str2 = reader2.readLine()) != null) {

if(!vector1.contains(str1)){

vector1.add(str1);

}

if(!vector2.contains(str2)){

vector2.add(str2);

}

}

reader1.close();

reader2.close();

} catch (IOException e) {

e.printStackTrace();

}

// 写入文件

String newFile1 = tempPath1;

String newFile2 = tempPath2;

File tempFile1 = new File(newFile1);

File tempFile2 = new File(newFile2);

if (tempFile1.exists()) {

tempFile1.delete();

}

if (tempFile2.exists()) {

tempFile2.delete();

}

tempFile1.createNewFile();

tempFile2.createNewFile();

try {

BufferedWriter writer1 = new BufferedWriter(new FileWriter(

tempFile1, true));

BufferedWriter writer2 = new BufferedWriter(new FileWriter(

tempFile2, true));

for (int i = 0; i 

str1 = (String) vector1.elementAt(i);

writer1.write(str1);

writer1.newLine();

writer1.flush();

}

for (int i = 0; i 

str2 = (String) vector2.elementAt(i);

writer2.write(str2);

writer2.newLine();

writer2.flush();

}

writer1.close();

writer2.close();

} catch (IOException e) {

e.printStackTrace();

}

}

public static void main(String[] args) throws IOException {

String tempPath1 = "D:\\tmp\\test1.txt";

String tempPath2 = "D:\\tmp\\test2.txt";

// String sourceFolder = "D:/tmp/tmp";

String resultPath1 = "D:\\tmp\\result1.txt";

String resultPath2 = "D:\\tmp\\result2.txt";

// File tempFile = new File(tempPath);

ResortByDel resort = new ResortByDel();

// resort.AllResortToTemp(sourceFolder,tempPath);//首先放到temp.txt,然后删除

resort.ResortToTemp(tempPath1, tempPath2, resultPath1, resultPath2);// 把temp文件再去重放入result文件

// resort.DelTemp(tempPath);//删除temp.txt中转文件

System.out.println("OK");

}

}

这样写

------解决方案--------------------

不好意思上面的有点问题,没注意集合存储问题,抱歉,不过代码还是有好多问题,请大家指正

package com.wwy.io;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class CopyFile {

/**

* @param args

*/

@SuppressWarnings("unchecked")

public static void main(String[] args) {

// TODO Auto-generated method stub

List mylist = read();

List my = read();

List l1 = repetition(mylist.get(0), mylist.get(1));

List l2 = repetition(my.get(1),my.get(0));

write(l1,l2);

System.out.println("ok");

}

public static List read(){

File f1 = new File("D:\\a\\test1.txt");

File f2 = new File("D:\\a\\test2.txt");

List mylist = new ArrayList();

List l1 = new ArrayList();

List l2 = new ArrayList();

String str1 = "";

String str2 = "";

BufferedReader br1 = null;

BufferedReader br2 = null;

try {

br1 = new BufferedReader(new FileReader(f1));

br2 = new BufferedReader(new FileReader(f2));

while((str1 = br1.readLine() ) != null ){// (str2 = br2.readLine()) != null){

l1.add(str1);

}

while((str2 = br2.readLine()) != null){

l2.add(str2);

}

mylist.add(l1);

mylist.add(l2);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

if(br1 != null){

br1.close();

br1 = null;

}

if(br2 != null){

br2.close();

br2 = null;

}

} catch (Exception e2) {

// TODO: handle exception

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值