如何使用Java在多个线程中复制文件
我正在学习Java中的多线程,我的任务是使用2+个线程复制文件 文件扩展名并不重要。所以我试着用两个线程来完成。 在第一个线程中,我复制文件的前半部分,在第二个线程中-第二个线程中。 我尝试将部分保存在字节数组中,然后转到主线程并使用ByteArrayOutputStream连接这些数组&保存源文件的副本。但它三分之二,六分之二有效。我不知道怎么了。如何使用Java在多个线程中复制文件如何使用Java在多个线程中复制文件,java,multithreading,file,core,Java,Multithreading,File,Core,我正在学习Java中的多线程,我的任务是使用2+个线程复制文件 文件扩展名并不重要。所以我试着用两个线程来完成。 在第一个线程中,我复制文件的前半部分,在第二个线程中-第二个线程中。 我尝试将部分保存在字节数组中,然后转到主线程并使用ByteArrayOutputStream连接这些数组&保存源文件的副本。但它三分之二,六分之二有效。我不知道怎么了。如何使用Java在多个线程中复制文件 public class FirstThread implements Runnable { private
public class FirstThread implements Runnable {
private byte[] part1thread;
private RandomAccessFile file;
public FirstThread(RandomAccessFile file, int byteArraySize) {
this.file = file;
this.part1thread = new byte[byteArraySize];
}
public void run() {
try {
System.out.println("Start the first thread.");
file.read(part1thread, 0, part1thread.length);
System.out.println("I am the first thread and I read first part1thread of the file you gave me.");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
RandomAccessFile fileOutputWrite = new RandomAccessFile("copy_", "rw");
System.out.println("First threads results writing...");
outputStream.write(this.part1thread);
System.out.println("output 1 thread: " + outputStream);
fileOutputWrite.write(this.getPart1thread(), 0, this.getPart1thread().length);
fileOutputWrite.close();
outputStream.close();
Main.setCountDownLatch();
} catch (IOException e) {
e.printStackTrace();
}
}
public byte[] getPart1thread() {
return part1thread;
}
}
}
}
我尝试使用*.pdf和*.txt文件。*。pdf副本无法打开,
*.txt文件:当我只复制上半部分或下半部分时,效果很好。
但当我试图一个接一个地复制两个部分时,它有时会起作用。
我不知道如何修复它。我添加了方法
public boolean getState() {
return Thread.currentThread().isAlive();
}
进入第一线程类
我让firstThread和secondThread成为主类的成员
if (Main.getFirstThread().getState()) {
我在SecondThread的run()方法中添加了if条件
Else-我们递归地尝试再次运行run()方法,直到第一个线程启动
哈哈,我需要
resultFile.seek(resultFile.length() - 0);
当我编写复制的文件时,如果没有“-0”,它将无法工作。请检查此60行程序(内联注释):
输出:
t2 start...
t1 start...
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t1, and I copied 4096 bytes from src file into pipe.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t1, and I copied 4096 bytes from src file into pipe.
I am t2, and I copied 1024 bytes from pipe into dest file.
// ...
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t1, and I copied 782 bytes from src file into pipe.
Reading from source file completed. Total: 6980366 bytes.
t1 done.
I am t2, and I copied 782 bytes from pipe into dest file.
Writing to dest file completed. Total: 6980366 bytes.
t2 done.
另见:
cos如果没有“-0”,它将不起作用。
这是不可能的,它不会对Java运行时产生影响,或者这将是一个中断的运行时。是的,这很奇怪,非常奇怪,但非常感谢!
resultFile.seek(resultFile.length() - 0);
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class SOQ54295781 {
private static final int BUFF_SIZE = 4096;
public static void main(final String[] args) throws IOException {
// create a "piped stream":
final PipedOutputStream output = new PipedOutputStream();
final PipedInputStream input = new PipedInputStream(output);
//"read" thread:
Thread thread1 = new Thread(() -> {
System.out.println("t1 start...");
//opens source file ...
try (FileInputStream fileIn = new FileInputStream("test.pdf")) { //args[0]
// ... and copies to pipe.
long totalBytes = copy(fileIn, output, "t1", "from src file into pipe");
System.out.format("Reading from source file completed. Total: %d bytes.%n", totalBytes);
fileIn.close();
} catch (IOException e) {
e.printStackTrace(System.err);
} finally {
try {
output.close();
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
System.out.println("t1 done.");
});
//"write" thread:
Thread thread2 = new Thread(() -> {
System.out.println("t2 start...");
// opens dest file ...
try (FileOutputStream fileOut = new FileOutputStream("test.copy.pdf")) {//args[1]
// ... and copies from pipe.
long totalBytes = copy(input, fileOut, "t2", "from pipe into dest file");
System.out.format("Writing to dest file completed. Total: %d bytes.%n", totalBytes);
fileOut.close();
} catch (IOException e) {
e.printStackTrace(System.err);
} finally {
try {
input.close();
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
System.out.println("t2 done.");
});
thread1.start();
thread2.start();
}
// thx to https://stackoverflow.com/a/22128215/592355 resp. guava extended by some debug output
private static long copy(InputStream from, OutputStream to, String thread, String msg)
throws IOException {
byte[] buf = new byte[BUFF_SIZE];
long total = 0;
while (true) {
int r = from.read(buf);
if (r == -1) {
break;
}
to.write(buf, 0, r);
total += r;
System.out.format("I am %s, and I copied %d bytes %s.%n", thread, r, msg);
}
return total;
}
}
t2 start...
t1 start...
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t1, and I copied 4096 bytes from src file into pipe.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t1, and I copied 4096 bytes from src file into pipe.
I am t2, and I copied 1024 bytes from pipe into dest file.
// ...
I am t2, and I copied 1024 bytes from pipe into dest file.
I am t1, and I copied 782 bytes from src file into pipe.
Reading from source file completed. Total: 6980366 bytes.
t1 done.
I am t2, and I copied 782 bytes from pipe into dest file.
Writing to dest file completed. Total: 6980366 bytes.
t2 done.