~~~
/**
ObjectInputStream、
ObjectOutputStream 通過在流中使用文件可以實現對象的持久存儲。
*/
import java.io.*;
public class ObjectStreamDemo{
public static void main(String args[])throws Exception{
//WriteObj();
ReadObj();
}
public static void ReadObj()throws Exception{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("obj.txt"));
Person p = (Person)ois.readObject();
System.out.println(p);
}
public static void WriteObj()throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
oos.writeObject(new Person("name",18));
oos.close();
}
}
~~~
~~~
/**
管道輸入流應該連接到管道輸出流;管道輸入流提供要寫入管道
輸出流的所有數據字節。通常,數據由某個線程從 PipedInputStream
對象讀取,并由其他線程將其寫入到相應的 PipedOutputStream。
不建議對這兩個對象嘗試使用單個線程,因為這樣可能死鎖線程。
管道輸入流包含一個緩沖區,可在緩沖區限定的范圍內將讀操作和寫操作分離開。
如果向連接管道輸出流提供數據字節的線程不再存在,則認為該管道已損壞。
*/
import java.io.*;
class Read implements Runnable{
private PipedInputStream pis;
public Read(PipedInputStream i){
this.pis = i;
}
public void run(){
byte []b = new byte[1024];
try{
int line = pis.read(b);
String info = new String(b,0,line);
System.out.println("content:"+info);
pis.close();
}catch(IOException e){
throw new RuntimeException("管道流讀取失敗!");
}
}
}
class Write implements Runnable{
private PipedOutputStream pos;
public Write(PipedOutputStream o){
this.pos = o;
}
public void run(){
String n = "hello world!";
try{
pos.write(n.getBytes());
pos.close();
}catch(IOException e){
throw new RuntimeException("管道流寫入失敗!");
}
}
}
public class PipStream{
public static void main(String args[])throws Exception{
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
pos.connect(pis);
Read r = new Read(pis);
Write w = new Write(pos);
new Thread(r).start();
new Thread(w).start();
}
}
~~~
~~~
/**
RandomAccessFile類支持對隨機訪問文件的讀取和寫入
其中的seek方法可以從文件中的指定位置讀取
同時該類 還提供RandomAccessFile(String name, String mode) 的
構造方法,mode是設置權限r,rw等.
其中提供了各種各樣的write和read方法,具體可以查看文檔.
當文件設置成r時,如果文件不存在,不能自動創建文件。
如果為rw時,如果文件不存在則自動創建文件,如果文件存在,則覆蓋該文件
------該類可以應用于多線程的讀寫文件--->及多線程下載功能
*/
import java.io.*;
public class RandomAccessFileDemo{
public static void main(String args[])throws IOException{
//writeRan();
readRan();
}
public static void readRan()throws IOException{
RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
int i=0;
byte[] b= new byte[6];
while(i<5){
raf.seek(i*10);
int len = raf.read(b);
String name = new String(b,0,len);
int age = raf.readInt();
System.out.println("name="+name+"::age="+age);
i++;
}
raf.close();
}
public static void writeRan()throws IOException{
RandomAccessFile raf = new RandomAccessFile("ran.txt","rw");
for(int i=0;i<5;i++){
// raf.seek(20*i); //設置每次寫入不能超過20字節
raf.write(("李四"+i).getBytes());
raf.writeInt(i*10);
}
raf.close();
}
}
~~~
~~~
/*
DataInputStream 和 DataOutputStream
該類便于讀寫java類型的基本數據類型
*/
import java.io.*;
public class DataStreamDemo{
public static void main(String args[])throws IOException{
//writeFile();
readFile();
}
public static void readFile()throws IOException{
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
int a = dis.readInt();
double d = dis.readDouble();
float f = dis.readFloat();
boolean b = dis.readBoolean();
System.out.println("a="+a+"-d="+d+"-f="+f+"-b="+b);
dis.close();
}
public static void writeFile()throws IOException{
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeInt(23);
dos.writeDouble(23.222d);
dos.writeFloat(33.22f);
dos.writeBoolean(true);
dos.close();
}
}
~~~