java的讀寫操作

來源:互聯網
上載者:User

檔案都是以流的形式輸入輸出的,而位元組序列的對象作為流,這些位元組序列的來源和目的地可以是檔案.網路連接.記憶體塊,抽象類別InputStream和OutputStream構成了有結構的輸入輸出類得基礎.而面向Unicode形式的用Reader和Writer為基礎.另外流可以通過Closeable介面關閉,OutputStream和Writer可以通過介面Flushable清空流,另外要注意的是只有Writer實現了Appendable介面.

java將流操作分離成兩個操作:擷取流和將位元組組裝到資料類型

如:DataInputStream sd = new DataInputStream(new FileInputStream("filename"));

程式用於直接操作目標裝置所對應的類叫節點流類,程式也可以通過一個間接流類去調用節點流類,以達到更加靈活方便地讀寫各種類型的資料,這個間接流類就是封裝流類。

Java io學習總結
一. InputStream繼承關係圖


InputStream和OutputStream:
兩者都為抽象類別,在沒有處理流的情況下,每次只能從資料來源讀取一個位元組和向資料來源寫出一個位元組。

Reader和Writer:
兩者同樣為抽象類別,在沒有處理流的情況下,每次只能從資料來源讀取一個字元(2個位元組)和向資料來源寫出一個字元。

 

注意:

 

1. 流對象執行個體後,必須得關閉
2. 關於流的類,除了列印流,很多方法都拋出來異常,需認真對待

 

3. Sysem.in和System.out分別為InputStream和OutputStream的執行個體化對象
它們由InputStream和OutputStream的之類建立。
4.有關阻塞式對象還需進一步研究。
5.處理資料用資料流
6.有意識的去運用緩衝流

二. 節點流

節點流串連的是資料來源(檔案/記憶體/管道),他們可以直接通過無參建構函式執行個體化,而不需要像處理流那樣需要傳入一個節點流參數才可以。


FileInputStream和FileOutputStream :

分別從檔案中讀取位元組和向檔案中寫位元組,跟byte和byte數組關係密切

FileInputStream 的主要方法:
int read() throws IOException ; //返回讀取的一個位元組,最後返回-1

int read(byte[] b) throws IOException;
// b - 儲存讀取資料的緩衝區。
//返回讀入緩衝區的總數,讀取的內容儲存在byte數組中。
int read(byte[] b, int off, int len ) throws IOException;

Reader和Writer:
分別從檔案中讀取字字元和向檔案中寫字元,跟char和byte數組(String)關係密切
1.Reader的主要方法:
int read() throws IOException ; //返回讀取的一個字元,最後返回-1

int read(char[] cbuf) throws IOException;

// cbuf - 儲存讀取資料的緩衝區。

//返回讀入緩衝區的總數,讀取的內容儲存在char數組中。

int read(char[] cbuf, int off, int len ) throws IOException;

2.Writer的主要方法:
Void write (int c) throws IOException ; //寫入單個字元
Void write(char[] cbuf) throws IOException; //寫入字元數組

void read(char[] cbuf, int off, int len ) throws IOException;

Void write(String str) throws IOException; //寫入字串

Void write(String str,int off,int length) throws IOException ;

ByteArrayInputStream和ByteArrayOutpustStream:

ByteArrayOutputStream類是在建立它的執行個體時,程式內部建立一個byte型別數組的緩衝區(記憶體中),然後利用 ByteArrayOutputStream和ByteArrayInputStream的執行個體向數組中寫入或讀出byte型資料。在網路傳輸中我們往往 要傳輸很多變數,我們可以利用ByteArrayOutputStream把所有的變數收集到一起,然後一次性把資料發送出去。

 

通常和過濾流DataInputStream和DataOutputStream混合使用
CharArrayReader和CharArrayWriter:
用法跟ByteArrayInputStrea和ByteArrayOutputStream用法一樣。

三. 處理流

 
(一). 緩衝流
有BufferedInputStream和BufferedOutputStream,相應的有BufferedReader和BufferedWriter類提供緩衝流。
使用緩衝流動好處:
1. 能夠提搞讀寫速度,在一次訪問硬碟的時候可以讀取多條資料
2. 擁有節點流說沒有的readLine(),即讀取一行的方法
(二). 資料流
DataInputStream和DataOutputStream:
用於輸入輸出Java基礎資料型別 (Elementary Data Type),通常跟ByteArrayInputstream和ByteArrayOutputStream一起聯合使用,注意沒有DataReader和DataWriter類存在。
使用例子:
package com.ljp.io;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class DataIOTest {

/**DataInputStream和DataOutputStream,ByteArrayInputStream和ByteArrayOutputStream的使用
* @param args
*/
public static void main(String[] args) {
//在記憶體中開闢一個位元組數組,並且往裡面寫資料
ByteArrayOutputStream byOut=new ByteArrayOutputStream();
DataOutputStream dataOut=new DataOutputStream(byOut);
try {
dataOut.writeBoolean(true);
dataOut.writeDouble(5.654612121);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//從開闢的記憶體位元組數組中讀出內容
ByteArrayInputStream byIn=new ByteArrayInputStream(byOut.toByteArray()); //注意參數
DataInputStream dataIn=new DataInputStream(byIn);
try {
System.out.println(dataIn.available());
System.out.println(dataIn.readBoolean());
System.out.println(dataIn.readDouble());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
byOut.close();
dataOut.close();
byIn.close();
dataIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

(三).列印流
1.列印流只用輸出資料流,沒有輸入資料流,屬於處理流,構造方法需要傳入節點流對象。

2.提供了重載的print和pintln方法

3.PrintWriter和PrintStream的輸出操作不會拋出異常,使用者通過檢測錯誤狀態擷取
錯誤資訊。
4.PrintWriter和PrintStream有自動檔flush功能。

例子:

package com.ljp.io;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class PrintStreamTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
FileOutputStream fo=null;
try {
fo = new FileOutputStream("H:/io.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintStream ps=new PrintStream(fo);
if(ps!=null){
System.setOut(ps);
}

for(int i=0;i<10;i++){
System.out.println(Math.random());
}
try {
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

(四).物件流程(object流)
ObjectInputStream和ObjectOutputStream:

1.直接將Object寫入或寫出
2.translent關鍵字
3.serializable介面(標記性介面,無方法):把對象轉換成位元組流,將其序列化

例子:
package com.ljp.io;

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;

public class ObjectIOTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
T t=new T();
FileOutputStream fos=null;
ObjectOutputStream oos=null;
try {
fos=new FileOutputStream("D:/io.txt");
oos=new ObjectOutputStream(fos);
oos.writeObject(t);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

FileInputStream fis=null;
ObjectInputStream ois=null;
try {
fis = new FileInputStream("D:/io.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ois=new ObjectInputStream(fis);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
T tt=null;
try {
tt=(T)ois.readObject();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.print(tt.i+","+tt.d+","+tt.s);
}

}

class T implements Serializable{
int i=9;
double d=2.98;
String s="hello world";
transient int k=29; //transient關鍵字使變數不能序列化
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.