java字节流

一. 字节输入流:InputStream(抽象类,所有字节输入流的超类)              

  1.FileInputStream:    文件输入流                                     

FileInputStream fileIS = new FileInputStream(File file); 建立通道  

                                                                   

读取数据:                                                          

        fileIS.read()          从输入流中(只)读取一个数据字节(迭代器效果)

        fileIS.read(byte[] b)  传字节数组作为缓冲区 (存数据)           

            byte[] b = new byte[1024];//一般为1024的次方              

      int count = 0;                                                 

        while((count = fileIS.read(b)) != -1){    //循环调用read(),判断是否为-1

            System.out.println(new String(b,0,count)); //转字符串      

        }                                                

            fileIS.read(b)      //传递后,这样 byte[]中就含有内容,    

            new String(b);      //把字符数组转换为字符串输出           

    关闭资源:                                                         

        fileIS.close()         关闭此文件输入流(结尾一定要关闭)      

                                                                       

2.FileInputStream使用步骤:                                           

    1.找到目标文件                                                     

    2.创建通道                                                         

    3.【创建一个缓冲区】                                               

    4.读取数据                                                         

    5.关闭资源                                                         

                                                                       

二.字节输出流:OutputStream(抽象类,所有字节输出流的超类)           

  1.FileOutputStream:文件输出流                                       

    FileOutputStream fileOS = new FileOutputStream(File file);

    FileOutputStream fileOS = new FileOutputStream(File file,boolean append);

    (第二个构造方法,可以在原来基础上追加内容)                         

    fileOS.write();

        // 3.创建一个字节数组(不是缓存区)

        String string = "Hello World";

        byte[] b = string.getBytes();   //把字符串转换为字节数组

    // 4.写入数据

fileOS.write(b);                                   

  2.FileOutputStream使用步骤                                           

    1.找到目标文件                                                     

    2.创建通道                                                         

    3.【创建一个字符数组(不是缓冲区)】                                

    4.开始写入数据                                                     

    5.关闭资源                                                         

  3.FileOutStream使用注意事项:                                        

    1.FileOutputStream写数据时,如果目标文件不存在,会直接创建一个

    2.FileOutputStream写数据时,目标文件存在,会先将数据清空,再写入数据

    3.FileOutputStream写数据时,如果在原来数据后追加,需要使用FileOutputSteam

(File file,boolean append)构造方法,append如果为true表示可以追加,反之不行。

    4.使用FIleOutputStream写数据时,用write(int a)写数据,接收是int,实际上是只有一个字节的数据,只操作低八位,其余24位舍弃                          

                                                                       

原文地址:https://www.cnblogs.com/z-jun/p/6138324.html