多线程文件复制工具

package com.hanker;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ThreadCopyPic {

	public static void main(String[] args) throws Exception {
		//File file = new File("C:\\Users\\Administrator\\Pictures\\girl.jpg");
		File file = new File("C:\\abc.txt");
		System.out.println(file.length());
		int data1 = (int) (file.length()/2);
		int data2 = (int) (file.length() -data1);
		
		new Thread() {
			@Override
			public void run() {
				try {
					FileWriter writer = new FileWriter("d:\\xxx.txt",true);
					char [] ch = new char[data1];
					FileReader reader = new FileReader(file);
					reader.read(ch);
					writer.write(ch);
					reader.close();
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}.start();
		
		Thread.sleep(1000);
		new Thread() {
			@Override
			public void run() {
				try {
					FileWriter writer = new FileWriter("d:\\xxx.txt",true);
					char [] ch = new char[data2];
					FileReader reader = new FileReader(file);
					reader.skip(data1);
					reader.read(ch);
					writer.write(ch);
					reader.close();
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}.start();
		
	}
}
发布了91 篇原创文章 · 获赞 43 · 访问量 14万+

猜你喜欢

转载自blog.csdn.net/kongfanyu/article/details/103786652