首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

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

2012-08-21 
各位大神好~我是刚学java的一位学生~写了个java多线程复制的程序~不知道错在何处~还请各位大神指教!!谢谢

各位大神好~·我是刚学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();
}
}
}
[解决办法]

探讨

引用:

把错误贴出来了,这么长怎么看啦。
其实程序错误时没有的~·但是复制出来的文件不完整~·程序得运行6次才能得到一个完整的文件~·而且这个复制出来的文件总会比原来的大那么1M不到的空间~·

[解决办法]
Java code
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!

热点排行