1. 程式人生 > >JAVA中IO流體系和使用(IO流)

JAVA中IO流體系和使用(IO流)

1.IO流:

位元組流:

抽象父類:

1.InputStream

2.OutputStream

檔案流:

1.FileInputStream

2.FileOutputStream

位元組緩衝流:

1.BufferedInputStream

2.BufferedOutputStream

ps:位元組流可以操作任意型別的資料

字元流:

抽象基類:

1.Reader

2.Writer

轉換流:

1.InputStreamReader

2.OutputStreamWriter

   flush()

字元緩衝流:特點:可以按行讀寫資料

1.BufferedReader

  readLine() - String

2.BufferedWriter

  PrintWriter

i.特殊的功能:自動行重新整理

ii.print  println

ps:字元流只能操作文字型別的資料

物件流:

ObjectInputStream

1.readObject() -Object

ObjectOutputStream

1.writeObject()

序列化:

              1.物件資料à位元組資料序列化

實現:

讓物件所屬的型別實現Serializable介面

2.位元組資料à物件反序列化

注意的問題:

1.反序列化時造成版本不相容問題的原因以及解決辦法:在物件所屬的類中生成序列版本號

2.如果一個物件是可序列化的,這個物件所屬的類內部有其他的物件作為它的成員,那麼作為成員的這個物件也一定是可序列化的

3.如果一個物件內部的資料並非全部要持久化,此時可以用transient來修飾不持久化的資料


            RandomAccessFile檔案操作:

                    Java提供了一個可以對檔案隨機訪問的操作,訪問包括讀和寫操作。該類名為RandomAccessFile。該類的讀寫是基於指標的操作。

                    RandomAccessFile也叫做隨機訪問流

                    RandomAccessFile在對檔案進行隨機訪問操作時有兩個模式,分別為只讀模式(只讀取檔案資料),和讀寫模式(對檔案資料讀寫)

1.位元組流:

//建立輸入流/輸出流物件
FileInputStream fis = new
FileInputStream("fis.java");
FileOutputStream fos = new
FileOutputStream("copy1.txt");

//按照單個位元組複製
int by = -1;
while((by=fis.read())!=-1){
    //將by寫入目標檔案中
fos.write(by);
}

//按照位元組陣列複製檔案
byte[] bys = new byte[1024];
int  len = 0;
while((len=fis.read(bys))>0){
    //將讀取到的位元組陣列寫入目標檔案中
fos.write(bys, 0, len);
}
//關閉流物件
fis.close();
fos.close();

2.位元組緩衝流

//建立流物件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("1.jpg"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("2.jpg"));
//完成複製
byte[] bys = new byte[1024];
int len = -1;
while((len=bis.read(bys))!=-1){
    bos.write(bys, 0, len);
    bos.flush();
}

    bis.close();
    bos.close();

3.字元流

//建立輸入流物件
InputStreamReader isr = new InputStreamReader(new FileInputStream("love.txt"),"UTF-8");
//建立輸出流物件
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"),"UTF-8");

//讀取
//按照單個字元讀取
//    int ch = -1;
//    while((ch=isr.read())!=-1){
//       osw.write(chs, 0, len);
//    }
//按照字元陣列讀取
char[]  chs = new char[1024];
int len = -1;
while((len=isr.read(chs))!=-1){
    //char[]-->String
osw.write(chs, 0, len);
}
//釋放資源
isr.close();
osw.close();

4.字元緩衝流

//建立輸入,輸出流物件
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("fis.java")));
PrintWriter pw = new PrintWriter(new FileOutputStream("buffer.txt"),true);

//複製
String str = null;
while((str=br.readLine())!=null){
    pw.println(str);
}
//流關閉
br.close();
pw.close();

5.物件流

/           //User類成員變數
private String name;
private String pass;
//transient關鍵字:
//當一個屬性被transient修飾後,該屬性在進行物件序列化時其值會被忽略.
//忽略不必要的屬性值可以達到物件序列化"瘦身"的效果.
private transient String desc;
p
   * 將User物件序列化到檔案中
   * 要實現將User物件中的desc資料不寫入檔案
   */
User user = new User("starry","123","描述");
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("transient.txt"));

oos.writeObject(user);
oos.close();

//反序列化操作
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("transient.txt"));
User per = (User)ois.readObject();

System.out.println(per.getName()+per.getPass()+per.getDesc());

ois.close();

6.RandomAccessFile隨機訪問流

 RandomAccessFile ra = new RandomAccessFile("web/love.txt","rw");
    RandomAccessFile ra1 = new RandomAccessFile("web/love-copy.txt","rw");
    byte[] by = new byte[1024];
    int i= -1;
    while((i = ra.read(by)) != -1 ){
        ra1.write(by,0,i);
    }
    ra.close();
    ra1.close();
//建立RandomAccessFile物件
RandomAccessFile raf = new RandomAccessFile("raf.txt", "rw");

//移動指標偏移量並讀取偏移量之後的內容到臨時檔案中
raf.seek(3);
//建立臨時檔案
File tmp = File.createTempFile("copy", ".tmp");
//複製
RandomAccessFile raftmp = new RandomAccessFile(tmp, "rw");
byte[] bys = new byte[1024];
int len = -1;
while((len=raf.read(bys))!=-1){
    raftmp.write(bys, 0, len);
}

raf.seek(3);
//向raf.txt檔案中插入資料
raf.write("hello".getBytes());

//將臨時檔案中的內容複製到本檔案的最後
raftmp.seek(0);
len = -1;
while((len=raftmp.read(bys))!=-1){
    raf.write(bys, 0, len);
}
//刪除臨時檔案
tmp.deleteOnExit();

raf.close();
raftmp.close();