1.使用线程将某个目录下的文件复制到另一个目录下,在复制的过程中要显示出完成进度(eg:5%,10%。。。),要求由客户端决定源文件和目标文件。
package ThreadCopy;
import java.io.*;
public class ThreadCopy extends Thread{
private File srcFile;
private File destFile;
public ThreadCopy(){
}
public ThreadCopy(File srcFile,File destFile){
this.srcFile = srcFile;
this.destFile = destFile;
}
private boolean flag = true;
public void setFlag(boolean flag) {
this.flag = flag;
}
//获取目标文件的长度
//重写run()方法完成文件的复制工作
public void run(){
while(flag){
try {
RandomAccessFile rraf = new RandomAccessFile(srcFile,"r");
RandomAccessFile wraf = new RandomAccessFile(destFile,"rw");
long srcSize = srcFile.length();
//System.out.println(srcFileSize);
long destSize = destFile.length();
//找到读写的位置开始读
if(destSize != srcSize){
System.out.println("开始读取文件辣。。。");
rraf.seek(destFile.length());
byte[] b = new byte[1024];
int len = rraf.read(b);
wraf.seek(destFile.length());
wraf.write(b, 0,len);
// System.out.println(destSize);
// System.out.println(srcSize);
double jd= destSize*1.0/srcSize;
System.out.println(jd);
System.out.println("已复制:"+(int)(jd*100)+"%");
Thread.sleep(1000);
}else{
System.out.println("已复制100%");
System.out.println("复制成功。。。。");
setFlag(false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
写一个测试类模拟客户端:
package ThreadCopy;
import java.io.*;
public class Test {
public static void main(String[] args) {
File srcFile = new File("E:"+File.separator+"滑稽.png");
File destFile = new File("D:"+File.separator+srcFile.getName());
// System.out.println(srcFile.length());
// System.out.println(destFile.length());
ThreadCopy tc = new ThreadCopy(srcFile,destFile);
tc.start();
}
}
2. 将一个图片对象进行序列化,对象含有作者信息和图片本身的字节信息。
图片类:
package SerialImage;
import java.io.Serializable;
public class Image implements Serializable{
private String author;
private byte[] b;
private static final long serialVersionUID = 2L;
public Image(){
}
public Image(String author,byte[] b){
this.author = author;
this.b = b;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public byte[] getB() {
return b;
}
public void setB(byte[] b) {
this.b = b;
}
}
将图片进行序列化并将序列化的二进制信息写入image.txt文件中:
package SerialImage;
import java.io.*;
public class SerialImage {
public static void main(String[] args) {
//获得该图片的字节数组
File file = new File("e:"+File.separator+"滑稽.png");
InputStream is = null;
OutputStream os = null;
ByteArrayOutputStream baos = null;
ObjectOutputStream oos = null;
try {
is = new FileInputStream(file);
os = new FileOutputStream("d:"+File.separator+"呵呵.png");
//读取图片文件并将内容写入到内存区,获得图片的字节数组内容
byte[] b =new byte[1024];
int len =0;
baos = new ByteArrayOutputStream();
while((len=is.read(b)) != -1){
baos.write(b, 0, len);
}
byte[] imb = baos.toByteArray();
//创建图片对象
Image image = new Image("凉凉", imb);
//将图片进行序列化
os = new FileOutputStream("d:"+File.separator+"image.txt");
oos = new ObjectOutputStream(os);
oos.writeObject(image);
System.out.println("序列化成功!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
将图片进行序列化:
package SerialImage;
import java.io.*;
public class DeSerialImage {
public static void main(String[] args) {
//反序列化文件
ObjectInputStream ois = null;
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream("d:"+File.separator+"image.txt");
ois = new ObjectInputStream(is);
Image image = (Image)ois.readObject();
//得到该图片的字节数组并向d盘中复制
byte[] imb = image.getB();
os = new FileOutputStream("d:"+File.separator+"滑稽.png");
os.write(imb);
System.out.println("写入成功!!!!");
} catch (Exception e) {
e.printStackTrace();
}
}
}