A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© DreamStart 中级黑马   /  2015-8-8 21:51  /  240 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

import java.io.*;
public class DataStreamDemo {
        public static void main(String[] args) throws IOException{
                method_1();
        }
        /*
         * DataInputStream 类
         *   构造方法: 传递字节输入流
         * DataInputStream(InputStream in)传递子类
         *   FileInputStream("文件")
         * int readInt()读取4字节基本类型
         * 读取基本类型的时候,-1不能作为文件的结尾看待
         * 读取是基本类型文件末尾抛出异常 java.io.EOFException
         * EOF  end of file
         */
        public static void method_1(){
                DataInputStream dis = null;
                try{
                //创建基本类型输入流,传递字节输入流
                 dis =new
                                DataInputStream(new FileInputStream("c:\\data.txt"));
                int len = 0;
                //利用循环读取基本类型文件 int int int
                while(true){
                        len = dis.readInt();
                        System.out.println(len);
                }
                /*int x = dis.readInt();
                System.out.println(x);
               
                x = dis.readInt();
                System.out.println(x);
               
                double d = dis.readDouble();
                System.out.println(d);
               
                x = dis.readInt();
                System.out.println(x);*/
               
               
                }catch(IOException ex){
                       
                }finally{
                        try {
                                dis.close();
                        } catch (IOException e) {
                               
                                e.printStackTrace();
                        }
                }
        }
       
       
        /*
         * DataOutputStream类
         *   构造方法: 传递字节输出流
         * DataOutputStream(OutputStream out)传递子类
         *  FileOutputStream("文件")
         *  void writeInt(int )写出去的是4字节,先写高字节
         */
        public static void method()throws IOException{
                //创建基本类型输出流,传递字节输出流
                DataOutputStream dos = new
                                DataOutputStream(new FileOutputStream("c:\\data.txt"));
                //写int类型基本数据
                dos.writeInt(97);
               
                //dos.writeBoolean(true);
               
                dos.writeInt(-1);
               
                dos.writeInt(4);
                dos.close();
        }
}





1 个回复

倒序浏览
不错赞一个
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马