1.使用RandomAccessFile实现文件合成。

2.使用java中的串行化技术实现对象图(100只猫Cat集合,但是Cat的owner不需要串行化)的深度复制。

3.阐述串行化的过程,应用场景,串行ID的用途。

===========================================================================

使用RandomAccessFile实现文件合成。

//文件分离

package com.it18zhang15;


import java.io.File;

import java.io.FileOutputStream;

import java.io.RandomAccessFile;


public class FileSpilt {


/**

* @param args

* @throws Exception 

*/

public static void main(String[] args) throws Exception {

File srcFile=new File("d:/test/a.txt");

//定义分成几个文件

int number=3;

long totalLength=srcFile.length();

//每个文件的长度

int fileLength=(int) (totalLength/number);

//随机访问

RandomAccessFile raf=new RandomAccessFile(srcFile, "r");

//循环取出

for(int i=0; i<number; i++){

//定义一个起始位置和终止位置

int startIndex= i*fileLength;

int endIndex=0;

//判断是不是最后一个

if(i==(number-1)){

endIndex=(int)(totalLength-1);

}

else{

endIndex=(i+1)*fileLength -1;

}

//取出文件

FileOutputStream fos=new FileOutputStream(srcFile.getAbsolutePath()+"_"+i);

//System.out.println(srcFile.getParent());

//System.out.println(srcFile.getParentFile());

raf.seek(startIndex);

byte[] buf=new byte[2];

//System.out.println(buf.length);

//int len=endIndex+1-startIndex;

while(true){

int currentpoint=(int) raf.getFilePointer();

int remain = endIndex - currentpoint + 1 ;

//System.out.println(remain);

if(remain > buf.length){

raf.read(buf);

fos.write(buf);

}

else{

raf.read(buf,0,remain);

fos.write(buf,0,remain);

}

if(currentpoint>endIndex){

break;

}

}

fos.close();

System.out.println("循环"+i+"结束");

}

raf.close();

}


}

//文件合成

package com.it18zhang15;


import java.io.File;

import java.io.FileOutputStream;

import java.io.RandomAccessFile;


public class FileCompose {


/**

* @param args

* @throws Exception 

*/

public static void main(String[] args) throws Exception {

/*for(int i = 0 ; i < 10 ; i ++){

char c = raf.readChar();

System.out.println(c);

raf.skipBytes(0);

}

*/

int number=3;

byte[] buf=new byte[1024];

for(int i=0;i<number ;i++){

File srcFile=new File("d:/test/a.txt"+"_"+i);

long toltalLength=srcFile.length();

RandomAccessFile raf = new RandomAccessFile(srcFile, "rw");

//追加方式写入文件

FileOutputStream fos=new FileOutputStream("d:/test/acompose.txt",true);

raf.readFully(buf, 0, (int)toltalLength);

fos.write(buf, 0, (int)toltalLength);

raf.close();

fos.close();

}

System.out.println("over");

}

}


2.使用java中的串行化技术实现对象图(100只猫Cat集合,但是Cat的owner不需要串行化)的深度复制。

package com.it18zhang15;


import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.List;


public class CatCopeDemo {


/**

* @param args

* @throws Exception 

* @throws FileNotFoundException 

*/

@SuppressWarnings("unchecked")

public static void main(String[] args) throws Exception {

List<Cat> list=new ArrayList<Cat>();

for(int i=0; i<100 ;i++){

list.add(new Cat("核桃"+i,i));

}

///*

//写入串行

ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("d:/test/cat.txt"));

oos.writeObject(list);

oos.close();

//*/

List<Cat> list1=new ArrayList<Cat>();

///*

//读出串行

ObjectInputStream ois=new ObjectInputStream(new FileInputStream("d:/test/cat.txt"));

list1=(List<Cat>) ois.readObject();

ois.close();

//*/

//输出集合

for(int i=0; i<100 ;i++){

System.out.println(list1.get(i).getName()+"是"+list1.get(i).getAge()+"岁");

}

}

}

package com.it18zhang15;


import java.io.Serializable;


public class Cat implements Serializable {


/**

*/

//下面这一句是serializable 的版本,增加后即使后面增加属性,其他调用该类也不会报错。

private static final long serialVersionUID = -5446106533693934226L;

private String name;

private int age;

private transient Person owner;

public Cat() {

super();

}

public Cat(String name, int age, Person owner) {

super();

this.name = name;

this.age = age;

this.owner = owner;

}

public Cat(String name, int age) {

super();

this.name = name;

this.age = age;

}

public Person getOwner() {

return owner;

}

public void setOwner(Person owner) {

this.owner = owner;

}

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;

}


}

package com.it18zhang15;


public class Person {

private String name;

private int 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;

}

}

3.阐述串行化的过程,应用场景,串行ID的用途。

串行化(Serialization)是计算机科学中的一个概念,它是指将对象存储到介质(如文件、内存缓冲区等)中或是以二进制方式通过网络传输。之后可以通过反串行化从这些连续的字节(byte)数据重新构建一个与原始对象状态相同的对象,因此在特定情况下也可以说是得到一个副本,但并不是所有情况都这样。

1、该对象的类必须实现Serializable接口。

2、该对象的串行化成员必须是非静态成员变量,即不能保存任何的成员方法和静态的成员变量,而且串行化保存的只是变量的值,对于变量的任何修饰符,都不能保存。而对于某些类型的对象,其状态是瞬时的,这样的对象是无法保存其状态的,例如一个Thread对象,或一个FileInputStream对象,对于这些字段,我们必须用transient关键字标明 。 (注:保存任何的成员方法和静态的成员变量没有任何的意义,因为对象的类已经完整的保存了他们)

3、要串行化一个对象,必须与一定的对象输入/输出流联系起来,通过对象输出流将对象状态保存下来,再通过对象输入流将对象状态恢复。

串行化有但不局限优点:

  1. 串行化是一种更好的使用类持久化的方法

  2. 可用于远程方法调用 如soap

  3. 一种分布对象的方法,特别是在软件组件中,如com corba

  4. 在随时间变化的数据检测改变