20180123: 作业1利用线程模拟文件的下载、作业2序列化与反序列化

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();
		}
		
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值