DataInputStream 數據類型數據輸入輸出流

package IOliu;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataInputStreamDemo {

    public static void main(String[] args) {
        String name = "張三";
        int age = 23;
        String email = "892524460@qq.com";
        String phone = "13165044534";
        
        //數據型數據的輸入輸出流
        FileOutputStream fos = null;
        FileInputStream fis = null;
        DataOutputStream  dos = null;
        DataInputStream dis = null;
        try {
            try {
                //生成新文件user    
                fos = new FileOutputStream("D:\\user.txt");
                dos = new DataOutputStream(fos);    //DataOutputStream中放的是對象
                //將數據輸出到user中         UTF String類型
                dos.writeUTF(name);
                dos.writeInt(age);
                dos.writeUTF(email);
                dos.writeUTF(phone);
                
                //輸入user中的數據
                fis = new FileInputStream("D:\\user.txt");
                dis = new DataInputStream(fis);
                String uName = dis.readUTF();
                int uAge = dis.readInt();
                String uEamil = dis.readUTF();
                String uPhone = dis.readUTF();
                System.out.println("姓名:"+uName+"年齡:"+uAge+"郵箱:"+uEamil+"電話:"+uPhone);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }  finally{
            try {
                fos.close();
                fis.close();
                dos.close();
                dis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}
相關文章
相關標籤/搜索