黑马程序员--Java IO流操作(3)

------- android培训java培训、期待与您交流! ----------

1.文件切割:

设计的知识:FileInputStream、Propertier、FileOutputStream
原理:将文件的内容按照指定大小分割成碎块文件。
private static void SplitFile(File file,String path) throws IOException {
		// TODO Auto-generated method stub
		//用读取流关联源文件
		FileInputStream fis = new FileInputStream(file);
		//定义1MB的缓冲区
		byte[] buf = new byte[SIZE];
		int len = 0;
		int count =1;
		
		//文件切割
		FileOutputStream fos = null;
		File dir = new File(path);
		if(!dir.exists()){
			dir.mkdir();
		}
		while((len=fis.read(buf))!=-1){
			fos = new FileOutputStream(new File(dir, (count++)+".part"));//分别写入不同的文件
			fos.write(buf, 0, len);
			fos.close();
		}
		//设置配置文件信息并保存
		Properties prop = new Properties();
		prop.setProperty("FileName", file.getName());
		prop.setProperty("PartCount", (count-1)+"");
		fos =new FileOutputStream(new File(dir,count+".properties"));
		prop.store(fos, file.getName());
		
		fos.close();
		fis.close();
		
	}

2.文件合并

设计的知识:ArrayList、FileOutputStream、SequenceStream、Propertier、FilenameFilter、Enumeration
private static void MergeFile(File dir) throws IOException {
		// TODO Auto-generated method stub
		
		ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
		
		//获取配置文件对象
		File[] files = dir.listFiles(new SuffixFilter(".properties"));
		if(files.length!=1){
			throw new RuntimeException(dir+"目录下没有properties扩展名的文件或者文件不唯一");
		}
		File config = files[0];
		
		Properties prop = new Properties();
		FileInputStream fis = new FileInputStream(config);
		prop.load(fis);
		
		//获取配置文件信息
		String filename = prop.getProperty("FileName");
		int Count =Integer.parseInt(prop.getProperty("PartCount"));
		
		//获取碎片文件信息
		File[] partFiles = dir.listFiles(new SuffixFilter(".part"));
		if(partFiles.length!=Count){
			throw new RuntimeException(dir+"碎片文件不全");
		}
		for(File file:partFiles){
			list.add(new FileInputStream(file));
		}
		//获取序列流对象
		Enumeration<FileInputStream> en = Collections.enumeration(list);
		SequenceInputStream sis = new SequenceInputStream(en);
		
		FileOutputStream fos = new FileOutputStream(new File(dir,filename));
	
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = sis.read(buf))!=-1){
			fos.write(buf, 0, len);
			fos.flush();
		}
		sis.close();
		fos.close();
	}
调用方法:
public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
//		File file = new File("E:\\1.mp3");
//		SplitFile(file,"E:\\part");
		File dir = new File("E:\\part");
		MergeFile(dir);
	}

3.对象的序列化与反序列化

java.io
类 ObjectOutputStream

java.lang.Object
  继承者 java.io.OutputStream
      继承者 java.io.ObjectOutputStream
所有已实现的接口:
Closeable, DataOutput, Flushable, ObjectOutput, ObjectStreamConstants

public class ObjectOutputStream
    
    
     
     extends 
     
     OutputStream
    
    
    
    
     
     implements 
     
     ObjectOutput, 
     
     ObjectStreamConstants
    
    
 
 

ObjectOutputStream 将 Java 对象的基本数据类型和图形写入 OutputStream。可以使用 ObjectInputStream 读取(重构)对象。通过在流中使用文件可以实现对象的持久存储。如果流是网络套接字流,则可以在另一台主机上或另一个进程中重构对象。

只能将支持 java.io.Serializable 接口的对象写入流中。每个 serializable 对象的类都被编码,编码内容包括类名和类签名、对象的字段值和数组值,以及从初始对象中引用的其他所有对象的闭包。 

public static void writeobj() throws IOException, FileNotFoundException {
		//对象的序列化
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\obj.txt"));
		oos.writeObject(new Person("张三", 30));
		oos.close();
	}

public class Person implements Serializable{//继承<span style="font-family: Arial, Helvetica, sans-serif;">Serializable接口是关键</span>

	private String name;
	private int age;

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

java.io
类 ObjectInputStream

java.lang.Object
  继承者 java.io.InputStream
      继承者 java.io.ObjectInputStream
所有已实现的接口:
Closeable, DataInput, ObjectInput, ObjectStreamConstants

public class ObjectInputStream
    
    
     
     extends 
     
     InputStream
    
    
    
    
     
     implements 
     
     ObjectInput, 
     
     ObjectStreamConstants
    
    
 
 

ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化。

ObjectOutputStream 和 ObjectInputStream 分别与 FileOutputStream 和 FileInputStream 一起使用时,可以为应用程序提供对对象图形的持久存储。ObjectInputStream 用于恢复那些以前序列化的对象。其他用途包括使用套接字流在主机之间传递对象,或者用于编组和解组远程通信系统中的实参和形参。

ObjectInputStream 确保从流创建的图形中所有对象的类型与 Java 虚拟机中显示的类相匹配。使用标准机制按需加载类。

只有支持 java.io.Serializable 或 java.io.Externalizable 接口的对象才能从流读取。

readObject 方法用于从流读取对象。应该使用 Java 的安全强制转换来获取所需的类型。在 Java 中,字符串和数组都是对象,所以在序列化期间将其视为对象。读取时,需要将其强制转换为期望的类型。 

private static void readobj() throws IOException, ClassNotFoundException {
		// TODO Auto-generated method stub
		//对象的反序列化
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\obj.txt"));
	    Person p = (Person) ois.readObject();
	    System.out.println(p.getName()+":"+p.getAge());
	    ois.close();
	}

之前遇到一个问题,如果在person类中添加toString方法,反序列化的时候程序会报错:

Exception in thread "main" java.io.InvalidClassException: com.dao.Person; local class incompatible: stream classdesc serialVersionUID = 8265597960204907160, local class serialVersionUID = 8007827058232781310

两个类class字节码文件对象不一致,

因此,反序列化和序列化前后对象类的内容必须完全一致






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值