import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class InstantThread extends Thread{
final static File readFile = new File("D:\1.rar");
final static File writeFile = new File("E:\1.rar");
@Override
public void run() {
//被拷贝文件的大小
long length = readFile.length();
System.out.println("被拷贝的文件大小:"+length+"B");
while (true) {
try {
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
long currentLength = writeFile.length();
System.out.println("已拷贝:"+currentLength+"B");
System.out.println("剩余:"+(length - currentLength)+"B");
if (currentLength>=length) {
System.out.println("文件已经完成拷贝");
break;
}
}
}
public void copy(File in,File ot) throws IOException {
FileInputStream input = new FileInputStream(in);
FileOutputStream ouput = new FileOutputStream(ot);
int data = 0;
while ((data = input.read())!=-1) {
ouput.write(data);
}
input.close();
ouput.close();
}
public static void main(String[] args) {
InstantThread i = new InstantThread();
i.start();
try {
i.copy(readFile, writeFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
很好 恭喜你掌握了 复制粘贴
package csdn005;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class InstantThread extends Thread{
/**
* 定义一个最终静态的读取文件对象
*/
final static File readFile = new File("D:\1.rar");
/**
* 定义一个最终静态的写文件对象
*/
final static File writeFile = new File("E:\1.rar");
/**
* 重写 Thread 类的 run 方法
*/
@Override
public void run() {
//被拷贝文件的大小
long length = readFile.length();
System.out.println("被拷贝的文件大小:" + length + "B");
// 循环
while (true) {
try {
// 睡眠2秒
sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 获取写文件的长度
long currentLength = writeFile.length();
System.out.println("已拷贝:" + currentLength + "B");
System.out.println("剩余:" + (length - currentLength) + "B");
// 判断读取当前文件的大小是否大于原始文件大小
if (currentLength >= length) {
System.out.println("文件已经完成拷贝");
// 读取当前文件的大小等于原始文件大小,就退出while
break;
}
}
}
public void copy(File in,File ot) throws IOException {
// 获取文件 in 的输入流
FileInputStream input = new FileInputStream(in);
// 获取文件 ot 的输入流
FileOutputStream ouput = new FileOutputStream(ot);
int data = 0;
// 循环读取 input 流,直到读取完毕
while ((data = input.read())!=-1) {
// 将 input 流写给 output
ouput.write(data);
}
// 流关闭
input.close();
ouput.close();
}
public static void main(String[] args) {
InstantThread i = new InstantThread();
i.start();
try {
i.copy(readFile, writeFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}