各位大神好~·我是刚学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);
}
// 线程你没有停止,所以会一直运行下去
// 输出文件都是一样的名字,所以最后只有一个文件
// while (begin < end) {
// i = raf1.read(b, 0, b.length);
// raf2.write(i);
//
// begin = begin + size;
// }
} catch (Exception e) {
e.printStackTrace();
}
}
}
[解决办法]
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(1, (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 counter = 0; 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 = end - begin + 1; byte[] b = new byte[size]; raf1.skipBytes(begin-1); raf1.read(b); raf2.skipBytes(begin-1); raf2.write(b); } catch (Exception e) { e.printStackTrace(); } }}
[解决办法]
磁盘 IO 操作不要使用多线程处理!
你要知道磁头只有一个,并发能力为 0!