字节流

字节流

字节输出流OutputStream

/**
 * @ClassName OutputStreamExer
 * @Description 字节输出流练习
 * @Author renhongchang
 * @Date 2021/7/19 19:31
 * @Version 1.0
 * @Blog https://rhc-rgb.github.io
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Java.io.OutputStram抽象类是字节输出流的所有类的超类
 * public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
 * public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
 * public void write(byte[] b) :将 b.length字节从指定的字节数组写入此输出流。
 * public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。
 * public abstract void write(int b) :将指定的字节写入输出流。
 *
 * 注意:
 * close方法,当完成流的操作时,必须调用此方法,释放系统资源。
 */
public class OutputStreamExer {
    public static void main(String[] args) throws IOException {
        //写出字节
        exer01();
        //写出字节数组
        exer02();
        //写出指定长度字节数组
        exer03();
        //以指定名称写出数据
        exer04();
        //写出换行
        exer05();
    }

    /**
     * FileOutputStram 是文件输出流,用于将数据写到文件
     *
     * 构造方法:
     * FileOutputStream(File file) 创建文件输出流以写入指定的文件对象
     * FileOutputStream(String name)  创建文件输出流以指定的名称写入文件
     *
     * 当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有这个文件,会创建该文件。如果有这个文件,会清空这个文件的数据。
     */

    /**
     * 写出字节
     * 一次写多个字节:
     * 如果写的第一个字节是正数(0~127),那么实现的时候会查询ASCII表
     * 如果写的第一个字节是负数,那第一个字节会和第二个字节,两个字节组成一个中文显示,查询默认系统码表(GBK)
     * @throws IOException
     */
    private static void exer01() throws IOException {
        //创建文件对象
        File file = new File("out.txt");
        //创建文件输出流  //此处第二个参数使用 true,每次写数据不会覆盖前边数据,默认为false
        FileOutputStream fileOutputStream = new FileOutputStream(file,true);
        //将数据写入文件  每次写出一个字节
        fileOutputStream.write(-3);   //写出第一个字节
        fileOutputStream.write(23);   //写出第二个字节
        //刷新流
        fileOutputStream.flush();
        //关闭流
        fileOutputStream.close();
    }

    /**
     * 写出字节数组:
     *
     * @throws IOException
     */
    private static void exer02() throws IOException {
        //创建文件对象
        File file = new File("out.txt");
        //创建文件输出流  //此处第二个参数使用 true,每次写数据不会覆盖前边数据,默认为false
        FileOutputStream fileOutputStream = new FileOutputStream(file,true);
        //字符串转换成字节数组
        //getBytes(String charsetName)方法,使用指定字符集,将字符串提取成byte序列,并存储在byte数组中;
        // getBytes():使用平台默认字符集
        byte[] b = "java程序员".getBytes();

        //写出字节数组数据
        fileOutputStream.write(b);
        //刷新流
        fileOutputStream.flush();
        //关闭流
        fileOutputStream.close();
    }

    /**
     * 写出指定长度字节数组:
     *
     * @throws IOException
     */
    private static void exer03() throws IOException {
        //创建字节输出流对象
        FileOutputStream fileOutputStream = new FileOutputStream(new File("out.txt"));
        //创建字节数组
        byte[] bytes = "Hello world".getBytes();
        //将指定长度写入文件
        fileOutputStream.write(bytes,3,4);
        //刷新流
        fileOutputStream.flush();
        //关闭流
        fileOutputStream.close();
    }

    /**
     * 以指定名称写出数据:
     *
     * @throws IOException
     */
    private static void exer04() throws IOException {
        //创建字节输出流对象
        FileOutputStream fileOutputStream = new FileOutputStream("output.txt");
        //写出字节数据
        fileOutputStream.write(97);
        //关闭流
        fileOutputStream.close();
    }

    /**
     * 写出换行:
     * windows中换行符号是: \r\n
     * 回车符:\r
     * 换行符:\n
     *
     * @throws IOException
     */
    private static void exer05() throws IOException {
        //创建字节输出流对象
        FileOutputStream fileOutputStream = new FileOutputStream(new File("out.txt"));
        //创建字节数组
        byte[] b = {97,98,99,100,101,102,103};
        //写出换行
        for (int i = 0;i < b.length;i++){
            //写出一个字节
            fileOutputStream.write(b[i]);
            //写出一个换行,换行符号住哪换成数组写出
            fileOutputStream.write("\r\n".getBytes());
        }
        //关闭资源
        fileOutputStream.close();
    }

}

字节输入流InputStream

/**
 * @ClassName InputStreamExer
 * @Description 字节输入流练习
 * @Author renhongchang
 * @Date 2021/7/19 20:16
 * @Version 1.0
 * @Blog https://rhc-rgb.github.io
 */


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * java.io.InputStream 抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入流的基本共性功能方法。
 * public void close() :关闭此输入流并释放与此流相关联的任何系统资源。
 * public abstract int read() : 从输入流读取数据的下一个字节。
 * public int read(byte[] b) : 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。
 * 注意:
 * close方法,当完成流的操作时,必须调用此方法,释放系统资源。
 *
 * java.io.FileInputStream 类是文件输入流,从文件中读取字节。
 * 构造方法
 * FileInputStream(File file) : 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
 * FileInputStream(String name) : 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
 * 当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有该文件,会抛出 FileNotFoundException 。
 */

public class InputStreamExer {
    public static void main(String[] args) throws IOException {
        //读取字节
        exer01();
        //使用字节数组读取数据
        exer02();
    }

    /**
     * read 方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回 -1 ,
     *
     * 1、fileInputStream.read( );   读取一个字节
     * 2、i = fileInputStream.read( ); 把读取到的字节赋值给变量b
     * 3、(i = fileInputStream.read( )!=-1;   判断变量b是否等于-1
     *
     * @throws IOException
     */
    private static void exer01() throws IOException {
        //创建文件对象File
        File file = new File("out.txt");
        //创建输入流对象
        FileInputStream fileInputStream = new FileInputStream(file);
        //读取字节
        int i;
        //read()方法必须写在循环里边
        while ((i = fileInputStream.read()) != -1){
            System.out.println((char) i);
        }
        //关闭流
        fileInputStream.close();
    }

    private static void exer02() throws IOException {
        //创建文件字节输入流对象
        FileInputStream fileInputStream = new FileInputStream(new File("out.txt"));
        //定义数组作为容器
        byte[] bytes = new byte[2];
        //定义变量作为读取的有效个数
        int len;
        while((len = fileInputStream.read(bytes)) != -1){
            //每次读取后把数组打印成字符串
            System.out.println(Arrays.toString(bytes));
        }
        //关闭资源
        fileInputStream.close();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值