字节流学习

package cn.onloc.utils.IO;

import cn.onloc.utils.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

//public class ByteIO_1 {

    /** 字节流
     *  1、字节流是什么?
     *
     *  https://www.cnblogs.com/DONGb/p/7844123.html
     *
     *  继承体系:
     *  顶级抽象类:
     *      输入流: inputStream
     *      输出流: outputStream
     *
     */

    public static void main(String[] args) {

            /** 字节输出流
             *  public abstract class OutputStream extends Object implements Closable,Flushable
             *  Closeable : 实现close方法,表示流的操作需要关闭
             *  flushable : 实现刷新方法,清空内存中的数据
             *
             *  他是一个抽象类,进行写入操作时,需要先实例具体的输出流类型
             *  常用的输出流类型:
             *      最常用的是把一些数据输出到一个文件中,fileOutputStream.
             *      有些时候我们需要写入文件的不再是一个单独的字符串,而会是一个对象,这时就引入了ObjectOutputStream,对象输出流,也就是对象的序列化。
             *
             *  写入过程:
             *      1、可以一次写一个字节数组的量
             *
             *  理解记忆:所有自己来定义这个接口的过程
             *      最顶层的是OutPutStream ,但是他是一个抽象类,具体的实现需要去创建一个具体的实例。
             *      在所有的输出流中,所有的目的肯定都是为了把程序中的一些数据,写入到磁盘中,而磁盘中的数据是以文件的形式存在的。所以肯定会有一个类来关联file和OutpurStream
             *      这个类就是FileOutputStream. 这是一个具体的实例类,他会提供几个构造方法,常用的有两个,第一个是通过构造方法确认输出流具体输出到那个文件地址。
             *      File file = new File("D://a.txt"); //该文件存在则直接写,不存在则创建
             *      OutputStream os = new FileOutputStream(file); //创建好了OutputStream 对象,现在就差需要写入的内容了。
             *      String str = "hello world" ; //需要写入的内容,可能会是任何类型,但是不能是对象类型,因为对象类型有专门的关联类去操作
             *      byte[] b = str.getbytes(); //因为字节输出流接受的肯定是一个字节数组
             *      os.writer(b); //把准备好的字节写出到磁盘文件中。这里的weiter可以一次写入一个数组长度的内容,也可以一次写入一个字节,见下方代码
             *      os.close(); //流的日常关闭
             *
             *      java是一种面向对象的语言,而jdk所提供的一些关于流的操作其实是不能够很好的满足需要的,这时候就需要对相关的jdk类进行封装,所以
             *      就可以吧流按照这个方向分为两大类:
             *      1、能直接通过设备进行IO流的传递的,字节流:FIleInputStream FileOutputStream
             *      2、不能直接于IO流进行传递,底层也需要通过FileOutputStream 去创建对象来传递的过滤流:ObjectOutputStream
             *
             *      对ObjectOutputStream的理解:
             *      public class ObjectOutputStream extends OutputStream
             *      对象输出流,首先也会先去确定要写出到那个文件:
             *      File file = new File("D:// objectOut.txt");  //确认写出到的磁盘文件
             *      OutputStream os = new FileOutputStream(file);
             *      ObjectOutputStream oos = new ObjectOutputStream(os);
             *      os.writerObject(user);
             *
             *
             *
             */
            {
                //fileOutputStream
                 try {
                    File file = new File("D://新建文件夹/outTest.txt");
                    OutputStream os = new FileOutputStream(file, true); //接着以前的文本书写
                    String str = ",你好 world";
                    byte[] b = str.getBytes();
                    os.write(b);
                    os.close();

                    File file2 = new File("D://新建文件夹/outTest2.txt");
                    OutputStream os2 = new FileOutputStream(file2);
                    String str2 = "你好 world";
                    byte[] b2 = str2.getBytes();
                    for (int i = 0; i < b2.length; i++) {
                        os2.write(b2[i]);
                    }
                    os2.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //ObjectOutputStream
            try {
                User user = new User("张三", 23);
                File file = new File("D://新建文件夹/objectOut.txt");
                OutputStream os = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(os);
                oos.writeObject(user);
                oos.close();
                os.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

            }

            {
                //字节输入流: 把文件中的内容输入到程序中
                // FileInputStream

                try {
                    File file = new File("D://新建文件夹/outTest.txt");
                    InputStream fs = new FileInputStream(file);
                    int b = fs.read();
                    while (b != -1) {
                        b = fs.read();
                        System.out.print(b + " ,");
                        //228 ,189 ,160 ,229 ,165 ,189 ,32 ,119 ,111 ,114 ,108 ,100 ,44 ,228 ,189 ,160 ,229 ,165 ,189 ,32 ,119 ,111 ,114 ,108 ,100 ,-1
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

               File file1 = new File("D://新建文件夹/outTest.txt");
                try {
                    InputStream is = new FileInputStream(file1);
                    List<Byte> list = new ArrayList();
                    int b2 ;
                    byte[] arr = new byte[40];
                    while( (b2 = is.read(arr)) != -1) {
                        for(byte bb : arr) {
                            System.out.print((char) bb + ", "); //中文是两个字节,所以解析出来是乱码
                        }
                    }

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                //ObjectInputStream

                try {
                    File file = new File("D://新建文件夹/objectOut.txt");
                    InputStream is = new FileInputStream(file);
                    ObjectInputStream ois = new ObjectInputStream(is);
                    User user = (User) ois.readObject();
                    System.out.println(user); //User{age=23, name='张三', sex=null}
                    ois.close();
                    is.close();

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

    }
//}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值