日期:2014-05-20  浏览次数:20699 次

各位大神好~·我是刚学java的一位学生~·写了个java多线程复制的程序~不知道错在何处~·还请各位大神指教!!谢谢啦~~!!
package com.elvis.homework5;

import java.io.*;

public class CopyOfFileCopy2 {

public static void main(String[] args) throws Exception {
String Filein = "1.txt";
String Fileout = "2.txt";

RandomAccessFile raf = new RandomAccessFile(Filein, "rw");

int length = (int) (raf.length());
int per = length / 5;

System.out.println(length);
System.out.println(per);

Thread th1 = new Thread(new FileThread1(0, (1 * per), Filein, Fileout));
Thread th2 = new Thread(new FileThread1((1 * per + 1), (2 * per), Filein, Fileout));
Thread th3 = new Thread(new FileThread1((2 * per + 1), (3 * per), Filein, Fileout));
Thread th4 = new Thread(new FileThread1((3 * per + 1), (4 * per), Filein, Fileout));
Thread th5 = new Thread(new FileThread1((4 * per + 1), (5 * per), Filein, Fileout));
Thread th6 = new Thread(new FileThread1((5 * per + 1), length, Filein, Fileout));

th1.start();
th2.start();
th3.start();
th4.start();
th5.start();
th6.start();

}
}

class FileThread1 implements Runnable {
int begin, end;
String Filein, Fileout;

public FileThread1(int begin, int end, String Filein,
String Fileout) {
this.begin = begin;
this.end = end;
this.Filein = Filein;
this.Fileout = Fileout;
}

@Override
public void run() {


try {
RandomAccessFile raf1 = new RandomAccessFile(Filein, "rw");
RandomAccessFile raf2 = new RandomAccessFile(Fileout, "rw");
int size = 1024;
byte[] b = new byte[size];
int i;
while (begin < end) {
i = raf1.read(b, 0, b.length);
raf2.write(i);

begin = begin + size;
}
} catch (Exception e) {
e.printStackTrace();
}

}
}


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

import java.io.*;

public class CopyOfFileCopy2 {

public static void main(String[] args) throws Exception {
String Filein = "1.txt";
String Fileout = "2.txt";

RandomAccessFile raf = new RandomAccessFile(Filein, "rw");

int length = (int) (raf.length());
int per = length / 5;

System.out.println(length);
System.out.println(per);

Thread th1 = new Thread(new FileThread1(0, (1 * per), Filein, Fileout));
Thread th2 = new Thread(new FileThread1((1 * per + 1), (2 * per),
Filein, Fileout));
Thread th3 = new Thread(new FileThread1((2 * per + 1), (3 * per),
Filein, Fileout));
Thread th4 = new Thread(new FileThread1((3 * per + 1), (4 * per),
Filein, Fileout));
Thread th5 = new Thread(new FileThread1((4 * per + 1), (5 * per),
Filein, Fileout));
Thread th6 = new Thread(new FileThread1((5 * per + 1), length, Filein,
Fileout));

th1.start();
th2.start();
th3.start();
th4.start();
th5.start();
th6.start();

}
}

class FileThread1 implements Runnable {
int begin, end;
String Filein, Fileout;

public FileThread1(int begin, int end, String Filein, String Fileout) {
this.begin = begin;
this.end = end;
this.Filein = Filein;
this.Fileout = Fileout;
}

@Override
public void run() {
try {
RandomAccessFile raf1 = new RandomAccessFile(Filein, "rw");
RandomAccessFile raf2 = new RandomAccessFile(Fileout, "rw");
int size = 1024;
byte[] b = new byte[size];
int length;

while ((length = raf1.read(b, 0, 1024)) != -1) {
raf2.write(b, 0, length);