首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >具有不同线程中的读取器和写入器的Java copy文件(使用BlockingQueue)

具有不同线程中的读取器和写入器的Java copy文件(使用BlockingQueue)
EN

Stack Overflow用户
提问于 2018-06-06 03:58:55
回答 1查看 153关注 0票数 0

我正在尝试编写一个java程序来复制一个文件,读取器在一个线程中,写入器在另一个线程中。该程序不会显示任何错误,但复制的文件具有与原始文件不同的校验和。我找不到我在程序中犯的错误。该程序复制文本文件,他们似乎没有任何问题。当我尝试复制zip文件时,复制成功完成,但当我尝试解压缩复制的zip文件时,它显示zip文件已损坏。有人能帮我找出错误吗?

代码语言:javascript
复制
import java.io.*;
import java.util.concurrent.*;
import java.util.*;

class qu{
qu(byte[] b,int s){
    this.b=b;
    this.s=s;
} 
  byte[] b;
  int s;
 public String toString(){
     return s+"";
 }
}

class reader implements Runnable{
public byte[] b = new byte[1048576];
String inf;
int s;
long max;
private  BlockingQueue<qu> blockingQueue;
public reader(String inf,BlockingQueue<qu> blockingQueue,long max){
    this.inf=inf;
    this.max=max;
    this.blockingQueue=blockingQueue;
}

public void run(){
        RandomAccessFile in=null;
        try{
            in = new RandomAccessFile(inf,"rw");
            while((s=in.read(b))!=-1){
               blockingQueue.put(new qu(b,s));
            }

        }catch(Exception e){System.out.println(e);}
        finally{
            try{
                in.close();
            }catch(Exception e){System.out.println(e);}

        }


}
}

class writer implements Runnable{
public byte[] b = new byte[1048576];
String outf;
int s=0;
long max=0;
private  BlockingQueue<qu> blockingQueue;
public writer(String outf,BlockingQueue<qu> blockingQueue,long max){
    this.outf=outf;
    this.max=max;
    this.blockingQueue=blockingQueue;
}

public void run(){
        RandomAccessFile out;
        qu asd;
        System.out.println("Copying..");
        try{
            out = new RandomAccessFile(outf,"rw");
            while(out.getFilePointer()!=max){
                    asd=blockingQueue.take();
                    //System.out.println(new String(asd.b));
                    out.write(asd.b,0,asd.s);

            }
            out.close();
        }catch(Exception e){System.out.println(e);}

    }
}



class mul {

public static void main(String[] args) {
    String inf = args[0];
    String outf = args[1];
    File file =new File(inf);
    long max = file.length();
    BlockingQueue<qu> blockingQueue = new ArrayBlockingQueue<>(64);
    if(file.exists()){
        long start_time = System.nanoTime();
        Thread t1=new Thread(new reader(inf,blockingQueue,max));
        t1.start();
        Thread t2=new Thread(new writer(outf,blockingQueue,max));
        t2.start();
        try{
            t1.join();
            t2.join();
        }catch(Exception ex){System.out.println(ex);}
        long end_time = System.nanoTime();
        double difference = (end_time - start_time) / 1e6;
        System.out.println("Time taken: "+difference+"ms");
        System.out.println("Copy Completed..");

    }else{System.out.println("File not Found");}

}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-06 04:04:00

你需要复制你的字节数组,例如

代码语言:javascript
复制
public class Qu {
    private final byte[] b;
    private final int s;

    public Qu(byte[] b,int s) {
        this.b = Arrays.copyOf(b, b.length);
        this.s = s;
    } 

    public String toString(){
        return s+"";
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50708015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档