DataInputStream DataOutputStream


package onlyfun.caterpillar;
 
import java.io.*;
 
public class DataStreamDemo {
    public static void main(String[] args) {
        Student[] students = {new Student("Justin", 90), 
                              new Student("momor", 95), 
                              new Student("Bush", 88)}; 
        try { 
            DataOutputStream dataOutputStream = 
                new DataOutputStream( 
                         new FileOutputStream("data.dat")); 
            
            for(Student student : students) { 
               dataOutputStream.writeUTF(student.getName()); 
               dataOutputStream.writeInt(student.getScore()); 
            } 
            dataOutputStream.flush(); 
            dataOutputStream.close(); 
            
            DataInputStream dataInputStream = 
                new DataInputStream( 
                         new FileInputStream("data.dat")); 
             
            for(int i = 0; i < students.length; i++) { 
                String name = dataInputStream.readUTF(); 
                int score = dataInputStream.readInt(); 
                students[i] = new Student(name, score); 
                students[i].showData(); 
            } 
            dataInputStream.close(); 
        } 
        catch(IOException e) { 
            e.printStackTrace(); 
        } 
    }
}
以上轉載自Java Gossip

0 意見: