java 两个文件去重复_java输出两个文件中相同的字符串

展开全部

你   看   看   这   个  ,是不是符合你的要求

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class TestOne {

    /**

     * @param args

     */

    public static void main(String[] args) {

        String filepath1 = "F:\\1.txt";

        String filepath2 = "F:\\2.txt";

        List<String> res1txt = null;

        List<String> res2txt = null;

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        //输入62616964757a686964616fe78988e69d8331333332616336连续的个数

        System.out.print("Input series string size:");

        try {

            int temp = Integer.parseInt(br.readLine());

            res1txt = readFile(filepath1, temp);

            res2txt = readFile(filepath2, temp);

            //比较两个list中的相同值

            for (int i = 0; i < res1txt.size(); i++) {

                for (int j = 0; j < res2txt.size(); j++) {

                    if(res1txt.get(i).equals(res2txt.get(j))){

                        System.out.println(res1txt.get(i));

                    }

                }

            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if(br != null)

                    br.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

    }

    /**

     * 读取文件,把结果以输入的连续个数分割,并保存在List中

     * @param filepath

     * @param size

     * @return

     */

    public static List<String> readFile(String filepath, int size){

        //如果size不合法,则返回空

        if(size <= 0)

            return null;

        List<String> list = new ArrayList<String>();

        BufferedReader br = null;

        try {

            //读文件

            br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));

            String temp = null;

            while((temp = br.readLine()) != null){

                for (int i = 0; i < temp.length() - size; i++) {

                    list.add(temp.substring(i, i+size));

                }

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

                if(br != null)

                    br.close();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }

        return list;

    }

}

这里发不了代码,只能以文本的形式了

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class TestOne {

/**

* @param args

*/

public static void main(String[] args) {

String filepath1 = "F:\\1.txt";

String filepath2 = "F:\\2.txt";

List res1txt = null;

List res2txt = null;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

//输入连续的个数

System.out.print("Input series string size:");

try {

int temp = Integer.parseInt(br.readLine());

res1txt = readFile(filepath1, temp);

res2txt = readFile(filepath2, temp);

//比较两个list中的相同值

for (int i = 0; i < res1txt.size(); i++) {

for (int j = 0; j < res2txt.size(); j++) {

if(res1txt.get(i).equals(res2txt.get(j))){

System.out.println(res1txt.get(i));

}

}

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if(br != null)

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 读取文件,把结果以输入的连续个数分割,并保存在List中

* @param filepath

* @param size

* @return

*/

public static List readFile(String filepath, int size){

//如果size不合法,则返回空

if(size <= 0)

return null;

List list = new ArrayList();

BufferedReader br = null;

try {

//读文件

br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath)));

String temp = null;

while((temp = br.readLine()) != null){

for (int i = 0; i < temp.length() - size; i++) {

list.add(temp.substring(i, i+size));

}

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if(br != null)

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

return list;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值