一、序列化:
package sonyi;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
public class SerializeDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
write();//寫
read();//讀
}
//反序列化
public static void read(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("obj.txt"));
ArrayList<Person> arr = (ArrayList<Person>)ois.readObject();
for(Person p:arr){//按順序讀取對象
System.out.println(p);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(ois != null)
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//序列化
public static void write(){
ObjectOutputStream oos = null;
ArrayList<Person> arr = new ArrayList<Person>();//建立容器裝對象
try {
oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
arr.add(new Person("zhangsan", 25));
arr.add(new Person("lisi", 26));
arr.add(new Person("wangwu", 27));
oos.writeObject(arr);//将容器作為對象傳遞給流
//System.out.println(oos);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(oos != null)
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class Person implements Serializable{
private String name;
private int age;
Person(String name,int age){
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", 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;
}
}
二、ByteArrayStream
package io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
用于操作位元組數組的流對象:
ByteArrayInputStream:在構造的時候,需要接收資料源,而且資料源是一個位元組數組
ByteArrayOutputStream:在構造的時候,不用定義資料目的,因為該對象中已經内部封裝了可變長度的位元組數組
這就是資料目的地。
因為這兩個流對象都操作的數組,并沒有使用系統資源,是以不用進行close關閉。
在流操作的講解時:
源裝置:鍵盤(System.in)、硬碟(FileStream)、記憶體(ArrayStream)
目的裝置:控制台(System.out)、硬碟(FileStream)、記憶體(ArrayStream)
用流的讀寫思想來操作數組。
*/
public class ByteArrayStream {
public static void main(String[] args) {
// TODO Auto-generated method stub
//資料源
ByteArrayInputStream bis = new ByteArrayInputStream("abdcefg".getBytes());
//資料目的
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int by = 0;
while((by = bis.read()) != -1){
bos.write(by);
}
System.out.println(bos.size());
System.out.println(bos.toString());
//bos.writeTo(new FileOutputStream("a.txt"));
}
}
三、DataStream
package io;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
基本資料類型流對象:
DataInputStream與DataOutputStream:
可以用于操作基本資料類型的資料的流對象
*/
public class DataStreamDemo {
public static void main(String[] args) {
// writeData();
// readData();
writeUTFDemo();
readUTFDemo();
}
public static void readUTFDemo(){
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("utfdata.txt"));
String s = dis.readUTF();
System.out.println(s);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(dis != null)
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void writeUTFDemo(){
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("utfdata.txt"));
dos.writeUTF("你好");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(dos != null)
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void readData(){
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("data.txt"));
int num = dis.readInt();
boolean b = dis.readBoolean();
double d = dis.readDouble();
System.out.println("num = " + num);
System.out.println("b = " + b);
System.out.println("d = " + d);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(dis != null)
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void writeData(){
DataOutputStream dos = null;
try {
dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(124);
dos.writeBoolean(true);
dos.writeDouble(9887.123);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(dos != null)
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
四、RandowAccessFile
package io;
import java.io.RandomAccessFile;
/*
RandowAccessFile:(可以實作多線程對檔案的同時操作。比如下載下傳)随機通路檔案
該類不算是IO體系中子類,而是直接繼承自Object。
但是它是IO包中成員,因為它具備讀和寫功能,内部封裝了一個數組,
而且通過指針對數組的元素進行操作,可以通過getFilePointer擷取指針位置。
同時可以通過seek改變指針的位置。
其實完成讀寫的原理就是内部封裝了位元組輸入流和輸出流。
通過構造函數可以看出,該類自能操作檔案。
而且操作檔案還有模式:
隻讀r,讀寫rw等。
如果模式為隻讀,不會建立檔案,會去讀取一個已存在的檔案,如果該檔案不存在,則會出現異常
如果模式為讀寫,那麼該對象的構造函數要操作的檔案如果不存在,會自動建立,如果存在,不會覆寫
*/
public class RandowAccessFileDemo {
public static void main(String[] args){
try {
//writeFile();
//readFile();
writeFile_2();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void readFile() throws Exception{
RandomAccessFile raf = new RandomAccessFile("ran.txt", "r");
//調整對象中指針,可以随意設定
//raf.seek(8);//通路第二個對象了
//跳過指定的位元組數
raf.skipBytes(8);//隻能往後調,不能往回調
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf);
int age = raf.readInt();
System.out.println("name = " + name + ",age = " + age);
raf.close();
}
public static void writeFile_2() throws Exception{
RandomAccessFile raf = new RandomAccessFile("ran.txt", "rw");
//raf.seek(8*3);//跳到指定位置
raf.write("周期".getBytes());
raf.writeInt(103);
raf.close();
}
public static void writeFile() throws Exception{
RandomAccessFile raf = new RandomAccessFile("ran.txt", "rw");
raf.write("李四".getBytes());
raf.writeInt(97);
raf.write("王五".getBytes());
raf.writeInt(98);
raf.close();
}
}