垃圾代码记录——实现对比文件内容-多线程版

java版

package com.example.demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class Test2 {

	public static void main(String[] args) throws IOException {
		Runnable run1 = new MyRunnnmable1();
		Runnable run2 = new MyRunnnmable2();
		Runnable run3 = new MyRunnnmable3();
		Runnable run4 = new MyRunnnmable4();
		Thread thread1 = new Thread(run1);
		Thread thread2 = new Thread(run2);
		Thread thread3 = new Thread(run3);
		Thread thread4 = new Thread(run4);
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
	}

	public static void compare(String path, String name,String result) throws IOException {
		path = "C:/Users/xietong/Desktop/111";
		File outFiles = new File(path + File.separator + result);
		OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(outFiles));
		BufferedWriter writer = new BufferedWriter(write);
		int i = 0;
		File file = new File("C:/Users/xietong/Desktop/111/pidcf.txt");
		FileInputStream fisR = new FileInputStream(file);
		InputStreamReader isr = new InputStreamReader(fisR, "UTF-8");
		BufferedReader br = new BufferedReader(isr);
		String line = br.readLine();
		while (line != null) {
			i++;
			File p = new File(path + File.separator + name);
			FileInputStream fis = new FileInputStream(p);
			InputStreamReader is = new InputStreamReader(fis, "UTF-8");
			BufferedReader b = new BufferedReader(is);
			System.out.println(line + "  " + p.getName() + " " + i + "   当前线程:" + Thread.currentThread());
			String content = b.readLine();
			while (content != null) {
				if (content != null && content.contains(line)) {
					writer.write(content);
					writer.newLine();
					System.out.println("co");
				}
				content = b.readLine();
			}
			line = br.readLine();

			fis.close();
			is.close();
			b.close();
		}
		writer.flush();
		fisR.close();
		isr.close();
		br.close();
//		}
		write.close();
		writer.close();
	}

	private static String getLine(BufferedReader b) {
		String str = null;
		try {
			str = b.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}
}

class MyRunnnmable1 implements Runnable {

	@Override
	public void run() {
		try {
			Test2.compare(null, "al.txt", "汇总1.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

class MyRunnnmable2 implements Runnable {

	@Override
	public void run() {
		try {
			Test2.compare(null, "cmpay.txt", "汇总2.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

class MyRunnnmable3 implements Runnable {

	@Override
	public void run() {
		try {
			Test2.compare(null, "wx.txt", "汇总3.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

class MyRunnnmable4 implements Runnable {

	@Override
	public void run() {
		try {
			Test2.compare(null, "un.txt", "汇总4.txt");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值