1. 程式人生 > >數據操作流DataOutputStream、DataInputStream類

數據操作流DataOutputStream、DataInputStream類

ati lose read amd class spa static 例子 java

【例子1】


import java.io.DataOutputStream;  
import java.io.File;  
import java.io.FileOutputStream;    
import java.io.IOException;   

public class DataOutputStreamDemo{

    public static void main(String[] args) throws IOException{

        File file = new File("d:" + File.separator + "hello.txt");

        char[] ch = { "A", "B", "C" };

        DataOutputStream out = null;

        out = new DataOutputStream(new FileOutputStream(file));

        for(char temp : ch){

            out.writeChar(temp);

        }

        out.close();

    }

}

【運行結果】:


A B C

現在我們在上面例子的基礎上,使用DataInputStream讀出內容。

【例子2】


import java.io.DataInputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.IOException;  

public class DataOutputStreamDemo{

    public static void main(String[] args) throws IOException{

        File file = new File("d:" + File.separator + "hello.txt");

        DataInputStream input = new DataInputStream(new FileInputStream(file));

        char[] ch = new char[10];

        int count = 0;

        char temp;

        while((temp = input.readChar()) != "C"){

            ch[count++] = temp;
        }

        System.out.println(ch);

    }

}

【運行結果】:


AB

數據操作流DataOutputStream、DataInputStream類