当前位置:网站首页>The 2022-8-9 sixth group of input and output streams

The 2022-8-9 sixth group of input and output streams

2022-08-10 00:12:00 Assad Bodhi

IO流

Java中输入/Output streams Commonly used streams:

           字节输入流           字节输出流               字符输入流       字符输出流
   抽象基类    InputStream         OutputStream            Reader           Writer
   访问文件    FileInputStream     FileOutputStream        FileReader       FileWriter
   (节点流)
   缓冲流     BufferedInputStream BufferedOutputStream     BufferedReader   BufferedWriter
   (处理流)
   操作对象    ObjectInputStream   ObjectOutputStream

字节输入流:

 * 1.创建一个FileInputStream对象
 * 2.定义一个标记,Used to control the reading of the input stream
 * 3.循环读取,如果读取到了-1,Indicates that the end of the file was read,循环结束
 * 4.关闭资源.*****
一个一个的读

Create an array of bytes to output together

 注意:We found that a stream is gone after reading it,不能再读了.
 *      Called by default when a stream has been readmark和resetmethod to record and reset,
 *      The stream has been reset to the position where it was last read,
 *      So the content cannot be read again.It is not that the stream is closed after reading once.
点击查看代码

 @Test
    public void test01() {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("e:/aaa.txt");
            // 开始读的操作,read方法,返回值是int,当返回值为-1时,The end of the file was read
            // Read the end-of-file marker
            int read;
            // When the byte stream reads data, it is read byte by byte
            // 循环读取
            while((read = inputStream.read()) != -1) {
                System.out.print(read + " ");
            }
            System.out.println();
            System.out.println("读取完毕,再读一次....");
            // When the byte stream reads data, it is read byte by byte
            // 循环读取
            while((read = inputStream.read()) != -1) {
                System.out.print(read + " ");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                // 关闭流
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
* 注意:We found that a stream is gone after reading it,不能再读了.
 *
 *      Called by default when a stream has been readmark和resetmethod to record and reset,
 *      The stream has been reset to the position where it was last read,
 *      So the content cannot be read again.It is not that the stream is closed after reading once.
 *

字节输出流

  FileOutputStream构造器:
  boolean append参数:如果传入true,It means to add on the original basis,不覆盖
				   如果传入false,或者不传,覆盖原有内容

 写的操作,target file if it does not exist,会自动新建.	

文件的复制

点击查看代码
  public static void main(String[] args) {
        InputStream inputStream = null;      //文件的复制
        OutputStream outputStream = null;
        try {
            int len;
            byte[] bytes = new byte[10];
           inputStream =  new FileInputStream("D:/abc.txt");
           outputStream = new FileOutputStream("D:/ych.txt");
           while ((len=inputStream.read(bytes))!=-1){
               outputStream.write(bytes,0,len);
           }
            System.out.println("文件复制成功");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                if (Objects.nonNull(inputStream)){
                    inputStream.close();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            try {
                if (Objects.nonNull(outputStream)){
                    outputStream.close();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }


    }

字符流

输出数据

字符流

输入数据

 * 字符处理流(用的最多)
 * 缓冲流
 *
 * 只能处理纯文本文件:
 * .txt,.java,.html,.css.........
 *
 * Write a copy of a file using a buffered character stream

缓冲字符流

输入数据

缓冲字符流

输出数据

 *  外层流,内层流,The outer stream is closed,The inner stream will then be closed.

 * We will eventually put it all together.class文件打包,Deploy this package to the server.
 * 从始至终,.javaJust written by us programmers,给程序员看的.
 * .javaNot even involved in packaging,does not appear on the server.
 *
 * The operation and maintenance personnel go to the server to deploy the project,It's a bunch of deployments.class.
 * 我们的.propertiesProperties files are not involved in compilation.

序列化与反序列化:操作对象

 *  序列化:将对象写入到IO流中,Turns an object of the memory model into a byte number,
 *      可以进行存储和传输.
 *  反序列化:从IO流中恢复对象,Data that will be stored on a hard drive or received from a network
 *      Revert to the object model
 *  使用场景:所有可在网络上传输的对象都必须是可序列化的,
 *      否则会报错,All objects stored on disk must also be serializable.
 *
 * 序列化版本号:
 *     反序列化必须拥有class文件,但随着项目的升级,class文件也会升级
 *     Serialization guarantees compatibility before and after upgrades.
 *
 *     javaSerialization provides a version number
 *  The version number can be freely specified,如果不指定,JVM会根据类信息自己计算一个版本号,
 *  所以无法匹配,则报错!!!
 *
 *  不指定版本号,There is another danger,不利于JVM的移植,可能class文件没有改,
 *  但是不同的jvmThe calculation rules are different,导致无法反序列化
 *
 *  If only the method is modified,Deserialization is not affected,无需修改版本号
 *  Modified static variablesstatic,瞬态变量transient,Deserialization is also not affected,无需修改版本号

总结:

 *  1.所有需要网络传输的对象都需要实现序列化接口
 *  2.对象的类名、Instance variables are serialized;方法、类变量、transient变量不会被序列化
 *  3.如果想让某个变量不被序列化,可以用transient修饰
 *  4.序列化对象的引用类型成员变量,也必须是可序列化的,否则会报错
 *  5.反序列化时必须有序列化对象的class文件
 *  6.The same object is serialized multiple times,只有第一次序列化为二进制流,In the future, it just saves the serialized version number
 *  7.It is recommended to add a version number to all serializable classes,方便项目升级.
原网站

版权声明
本文为[Assad Bodhi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208092201061097.html