It 18 Palm Job _java Foundation 15th Day _IO serialization/deep copy

Source: Internet
Author: User
Tags object serialization

1. Use Randomaccessfile to achieve file compositing.

2. Use serialization technology in Java to implement an object graph (100 cat cat collections, but cat owner does not need serialization) for deep replication.

3. Describe the serialization of the process, application scenarios, the purpose of serial ID.


1.


Package Com.it18zhang;


/**

* Composition of segmented files

*/

Import Java.io.File;

Import Java.io.FileInputStream;

Import Java.io.RandomAccessFile;


public class App {


public static void Main (string[] args) throws Exception {

Defining a segmented small file class

file[] srcfile = new File[3];

Synthetic file path

File Desfile = new file ("D:/file.txt");


Randomaccessfile RAF = new Randomaccessfile (Desfile, "RW");


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

Srcfile[i] = new File ("D:/bb.txt" + "_" + i);

FileInputStream fis = new FileInputStream (Srcfile[i]);

byte[] buf = new byte[2];

int len = 0;

while (len = Fis.read (BUF))! =-1) {

Raf.write (buf, 0, Len);

}

Fis.close ();

}

Raf.close ();

System.out.println ("over");


}

}



Package Com.it18zhang;

/**

* File Splitter

*/

Import Java.io.File;

Import Java.io.FileOutputStream;

Import Java.io.RandomAccessFile;


public class Filesplitter {


public static void Main (string[] args) throws Exception {

source file

File Srcfile = new file ("D:\\bb.txt");

Random Access

Randomaccessfile RAF = new Randomaccessfile (srcfile, "R");


Total file length

Long totallength = Srcfile.length ();

Copies of documents

int number = 3;


Calculates the average length of each file, and the last copy

int filelength = (int) totallength/number;


Number of recycled copies, read and write files

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

System.out.println ("Loop:" + i);

Start pointer

int startIndex = i * filelength;

Processing End pointer

int endIndex = 0;

if (i = = (number-1)) {//Last copy

EndIndex = (int) totalLength-1;

} else {

EndIndex = ((i + 1) * filelength)-1;

}


Creating a file output stream

FileOutputStream fos = new FileOutputStream (Srcfile.getabsolutepath () + "_" + i);

Raf.seek (StartIndex);


Defining buffers

byte[] buf = new byte[2];


while (true) {

Get the current file pointer

int currpointer = (int) raf.getfilepointer ();

int remain = Endindex-currpointer + 1;

if (remain >= buf.length) {

Raf.read (BUF);

Fos.write (BUF);

} else {

Raf.read (buf, 0, remain);

Fos.write (buf, 0, remain);

}

if (Raf.getfilepointer () > EndIndex) {

Break

}

}

Fos.close ();

System.out.println ("End");

}

Raf.close ();

}

}



2.

Package com.it18zhang.deepcopy;


Import Java.io.FileInputStream;

Import Java.io.FileOutputStream;

Import Java.io.ObjectInputStream;

Import Java.io.ObjectOutputStream;

Import java.util.ArrayList;

Import java.util.List;


public class Appcatlist {


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 ("Tom" +i,i+2,i));

}

Write (serialization)

ObjectOutputStream oos = new ObjectOutputStream (New FileOutputStream ("D:/cats.dat"));

Oos.writeobject (list);

Oos.close ();

Read (crossdress line)

ObjectInputStream ois = new ObjectInputStream (New FileInputStream ("D:/cats.dat"));

List = (list<cat>) ois.readobject ();

Ois.close ();

for (Cat c:list) {

System.out.println (C.getname () + "-" +c.getage () + "-" +c.getxx ());

}

}


}


Package com.it18zhang.deepcopy;


Import java.io.Serializable;


public class Cat implements serializable{


Private static final long serialversionuid = 2781643003919236664L;

private String name;

private int age;

private String color;

private int xx;

Public Cat (String name, int age, int xx) {

Super ();

THIS.name = name;

This.age = age;

this.xx = XX;

}

A temporary

private transient person owner;

Private Dog Neibor;

Public Dog Getneibor () {

return Neibor;

}

public void Setneibor (Dog neibor) {

This.neibor = Neibor;

}

Public String GetName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

public int getage () {

return age;

}

public void Setage (int.) {

This.age = age;

}

Public String GetColor () {

return color;

}

public void SetColor (String color) {

This.color = color;

}

public int getxx () {

return xx;

}

public void setxx (int xx) {

this.xx = XX;

}

}


Package com.it18zhang.deepcopy;


Import java.io.Serializable;


public class person implements serializable{


Private static final long serialversionuid = 1513592729849839686L;

private String name;


Public String GetName () {

return name;

}


public void SetName (String name) {

THIS.name = name;

}

}

Package com.it18zhang.deepcopy;


Import java.io.Serializable;


public class Dog implements serializable{


Private static final long serialversionuid = -6027249953977477940l;

private String name;


Public String GetName () {

return name;

}


public void SetName (String name) {

THIS.name = name;

}

}



3. Describe the serialization of the process, application scenarios, the purpose of serial ID.


Serialization (serialization) is a concept in computer science that refers to storing objects in media (such as files, memory buffers, etc.) or transmitting them over a network in a binary way. You can then re-construct an object with the same state as the original object from these contiguous byte data by crossdress, so you can say that you have a copy in a particular case, but not all of it.

1. The class of the object must implement the Serializable interface.

2, the serialized member of the object must be a non-static member variable, that is, no member methods and static member variables can be saved, and serialization is only the value of the variable, for any modifier of the variable can not be saved.

3, to serialize an object, must be associated with a certain object input/output stream, through the object output stream to save the object state, and then through the object input stream to restore the object state.

The object serialization feature is simple and powerful, with applications in RMI, sockets, JMS, and EJBs. Object serialization problem is not the most exciting topic in network programming, but it is very important and has many practical meanings.





This article from "Rookie Achievement Data Road" blog, reproduced please contact the author!

It 18 Palm Job _java Foundation 15th Day _IO serialization/deep copy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.