1.使用RandomAccessFile實現文件切割。java
答:編程
package app_做業; import java.io.File; import java.io.FileOutputStream; import java.io.RandomAccessFile; public class app15_使用RandomAccessFile實現文件切割 { public static void main(String[] args) throws Exception { // 源文件 File srcFile = new File("D:/foo.txt"); // 隨機訪問 RandomAccessFile raf = new RandomAccessFile(srcFile, "r"); // 文件總長度 long totalLength = srcFile.length(); // 文件的分割份數 int number = 3; // 計算平均每一個文件的長度,最後一份 int fileLength = (int) totalLength / number; // 循環份數,讀寫文件 for (int i = 0; i < number; i++) { System.out.println("循環:" + 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); raf.seek(startIndex); // 定義緩衝區 byte[] buf = new byte[2]; while (true) { // 獲得當前文件指針 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("結束"); } raf.close(); } }
2.使用java中的串行化技術實現對象圖(100只貓Cat集合,可是Cat的owner不須要串行化)的深度複製。網絡
package com.it8zhang_15job.demo; 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()); } /******************** 寫入(串行) **********************/ //對象輸出流 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/cats.dat")); oos.writeObject(list); oos.close(); /******************** 讀取(反串行) **********************/ 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.it8zhang_15job.demo; import java.io.Serializable; /** * javaBean */ public class Cat implements Serializable { private static final long serialVersionUID = 1861516170581376793L; private String name; private int age; private String color ; private int xx ; //臨時的 private transient Person owner ; private Dog neibor ; public Dog getNeibor() { return neibor; } public void setNeibor(Dog neibor) { this.neibor = neibor; } public Person getOwner() { return owner; } public void setOwner(Person owner) { this.owner = owner; } public int getXx() { return xx; } public void setXx(int xx) { this.xx = xx; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public Cat() { System.out.println("()"); } public Cat(String name, int age) { super(); System.out.println("(1)"); 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; } } package com.it8zhang_15job.demo; import java.io.Serializable; public class Dog implements Serializable{ private static final long serialVersionUID = 264736816365982588L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } package com.it8zhang_15job.demo; import java.io.Serializable; public class Person implements Serializable{ private static final long serialVersionUID = -3860831553852022224L; private String name ; public String getName() { return name; } public void setName(String name) { this.name = name; } }
3.闡述串行化的過程,應用場景,串行ID的用途。app
答:串行化(Serialization)是計算機科學中的一個概念,它是指對象存儲到介質(如文件、內存緩衝區等)中或是以二進制方式經過網絡傳輸。以後能夠經過反串行化從這些連續的字節(byte)數據從新構建一個與原始對象相同的對象,所以在特定狀況下也能夠說是獲得一個副本,但並非全部狀況都是這樣。dom
一、該對象的類必須實現Serializable接口。ide
二、該對象的串行化成員必須是非靜態成員變量,即不能保存任何的成員方法和靜態的成員變量,並且串行化保存的是變量的值,對於變量的任何修飾符,都不能保存。this
三、要串行化一個對象,必須與必定的對象輸入/輸出流聯繫起來,經過對象輸出流將對象狀態保存下來,再經過對象輸入流將對象狀態恢復。spa
對象序列化功能很是簡單、強大,在RMI、Socket、JMS、EJB都有應用。對象序列化問題在網絡編程中並非最激動人心的課題,但卻至關重要,具備許多實用意義。指針