Java IO流 04:数据流 & 标准输出流

一、数据流

  • DataOutputStream:数据专属的输出流。
    DataInputStream:数据专属的输入流。

    • 这个流可以将数据连同数据的类型一并写入文件。
    • DataOutputStream写的文件,只能使用DataInputStream去读。并且读的时候你需要提前知道写入的顺序。
      ——读的顺序需要和写的顺序一致。才可以正常取出数据。
      注:这个文件不是普通文本文档。(这个文件使用记事本打不开。)

    ——DataOutputStream(输出,编写)

    public class DataOutputStreamTest01 {
        public static void main(String[] args) {
            //定义数据专属的字节输出流
            DataOutputStream dos = null;
            try {
                //创建数据专属的字节输出流
                dos = new DataOutputStream(new FileOutputStream("E:/data"));
                // 写数据
                byte b = 100;
                short s = 200;
                int i = 300;
                long l = 400L;
                float f = 3.0F;
                double d = 3.14;
                boolean sex = false;
                char c = 'a';
                //把数据以及数据的类型一并写入到文件当中。
                dos.writeByte(b);
                dos.writeShort(s);
                dos.writeInt(i);
                dos.writeLong(l);
                dos.writeFloat(f);
                dos.writeDouble(d);
                dos.writeBoolean(sex);
                dos.writeChar(c);
                //刷新
                dos.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (dos != null) {
                    try {
                        dos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    ——DataInputStream(输入,读取)

    public class DataInputStreamTest01 {
        public static void main(String[] args) {
            //定义数据专属的字节输入流
            DataInputStream dis = null;
            try {
                //创建数据专属的字节输入流
                dis = new DataInputStream(new FileInputStream("E:/data"));
                //输入顺序要与输出顺序一致,不然得到数据会不一致
                System.out.println(dis.readByte());
                System.out.println(dis.readShort());
                System.out.println(dis.readInt());
                System.out.println(dis.readLong());
                System.out.println(dis.readFloat());
                System.out.println(dis.readDouble());
                System.out.println(dis.readBoolean());
                System.out.println(dis.readChar());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally{
                if (dis != null) {
                    try {
                        dis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }	
    

二、标准输出流

  • PrintStream:标准的字节输出流,默认输出到控制台。
    • 标准输出流不需要手动close()关闭。
    • 可以改变标准输出流的输出方向。
    public class PrintStreamTest01 {
        public static void main(String[] args) {
            //联合起来写
            System.out.println("hallo world!");
            //拆分后System.out是PrintStream类型
            PrintStream out = System.out;
            out.println("hallo kitty!");
            /*
            System类中使用过的方法和属性:
            System.gc();---调用垃圾回收器
            System.currentTimeMillis();---获得当前系统时间的总毫秒数
            System.arraycopy();---数组的拷贝
            System.exit();---关闭系统
            PrintStream out = System.out;
             */
    
            //标准输出流不在指向控制台,指向"log"文件
            try {
                //创建一个标准字节输出流
                PrintStream log = new PrintStream(new FileOutputStream("log",true));
                //设置输出方向,将输出方向修改到"log"文件
                System.setOut(log);
                //输出
                System.out.println("hallo world!");
                System.out.println("hallo kitty!");
                System.out.println("设置输出方向成功!");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    
    💨扩展——日志工具
    public class Logger {
        public static void log(String msg) {
            try {
                //创建一个标准字节输出流,指向一个日志文件
                PrintStream out = new PrintStream(new FileOutputStream("src/com/javaSE/io/loggerUtil/log.txt",true));
                //设置输出方向
                System.setOut(out);
                //当前日期时间
                Date date = new Date();
                //设置日期时间格式
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
                //当前日期时间应用指定日期格式
                String s = sdf.format(date);
                //输出当前日期时间发生了什么事
                System.out.println(s+" : "+msg);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    //测试
    class LoggerTest {
        public static void main(String[] args) {
            Logger.log("用户尝试进行登录,验证失败");
            Logger.log("提取1000元失败,余额不足!");
        }
    }
    
  • PrintWriter:标准的字符输出流。(与PrintStream相似)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值