多线程实现文件备份和文件压缩


package com.xiancheng.test;

import java.io.IOException;

public class XianChen {
	
	public static String srcFilePath = "D://testXiancheng//上海.csv";
	public static String destFilePath = "D://testXiancheng//test//";
	public static String zipFilePath = "D://testXiancheng//test//";
	
	public static void main(String[] args) throws IOException{
		destFilePath = destFilePath + "test_" + System.currentTimeMillis() + ".csv";
		zipFilePath = zipFilePath + "test_" + System.currentTimeMillis() + ".zip";
		UpLoadFile uf = new UpLoadFile(srcFilePath,destFilePath);
		ZipFile zf = new ZipFile(srcFilePath,zipFilePath);
		Thread th1 = new Thread(uf);
		Thread th2 = new Thread(zf);
		th1.start();
		th2.start();
	}
	
}


package com.xiancheng.test;

import java.io.File;

public class GetFile {
	
	public static GetFile gf = null;
	
	private GetFile(){}
	
	public static GetFile getFile(){
		if(gf == null){
			gf = new GetFile();
		}
		return gf;
	}
	
	public File getFilePath(String filePath){
		File f = new File(filePath);
		return f;
	}

}



package com.xiancheng.test;

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

public class UpLoadFile implements Runnable {

	public String sFile;
	public String dFile;

	public UpLoadFile(String srcFile, String descFile) {
		this.sFile = srcFile;
		this.dFile = descFile;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		UpLoadFileFunc();
	}

	public synchronized void UpLoadFileFunc() {
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedWriter bw = null;
		try {
			GetFile gf = GetFile.getFile();
			// 输入输出文件
			File srcFile = gf.getFilePath(sFile);
			File destFile = gf.getFilePath(dFile);
			long start = System.currentTimeMillis();
			// 字节字符输入流
			fis = new FileInputStream(srcFile);
			isr = new InputStreamReader(fis);
			BufferedReader br = new BufferedReader(isr);
			// 字节字符输出流
			FileOutputStream fos = new FileOutputStream(destFile);
			OutputStreamWriter osw = new OutputStreamWriter(fos);
			bw = new BufferedWriter(osw);
			String s = br.readLine();
			while (s != null) {
				bw.write(s);
				bw.newLine();
				s = br.readLine();
			}
			long end = System.currentTimeMillis();
			System.out.println("备份耗时:" + (end - start));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				isr.close();
				if (bw != null) {
					bw.flush();
					bw.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}



package com.xiancheng.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipFile implements Runnable {

	public String sFile;
	public String dFile;

	public ZipFile(String srcFile, String descFile) {
		this.sFile = srcFile;
		this.dFile = descFile;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		ZipFileFunc();
	}

	public synchronized void ZipFileFunc() {
		ZipOutputStream zipOut = null;
		FileOutputStream out = null;
		FileInputStream in = null;
		try {
			long start = System.currentTimeMillis();
			GetFile gf = GetFile.getFile();

			// 输入输出文件
			File srcFile = gf.getFilePath(sFile);
			File destFile = gf.getFilePath(dFile);

			// 创建文件输入流对象
			in = new FileInputStream(srcFile);
			// 创建文件输出流对象(zip压缩文件)
			out = new FileOutputStream(destFile);
			// 创建ZIP数据输出流对象
			zipOut = new ZipOutputStream(out);
			// 得到文件名称
			String fileName = sFile.substring(sFile.lastIndexOf('/') + 1, sFile.length());

			// 创建指向压缩原始文件的入口
			ZipEntry entry = new ZipEntry(fileName);
			zipOut.putNextEntry(entry);

			// 向压缩文件中输出数据
			int number = 0;
			byte[] buffer = new byte[1024];
			while ((number = in.read(buffer)) != -1) {
				zipOut.write(buffer, 0, number);
			}
			long end = System.currentTimeMillis();
			System.out.println("压缩耗时:" + (end - start));
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				zipOut.close();
				out.close();
				in.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值