字节输入流/输出流

package cn.lianxi.test;

import org.junit.Test;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * 使用字节输出流写数据的步骤
 * */
public class Demo {
    @Test
    public void test01() throws Exception {
        File file = new File("file1.txt");

        //如果不存在 就创建
        if(!file.exists()){
            file.createNewFile();
        }
        //创建in输入流
        FileInputStream in = new FileInputStream(file);

        int i = -1;
        while ((i = in.read()) != -1){
            System.out.println((char)i);
        }

        in.close();
    }

    @Test
    public void test02() throws Exception {
        //创建out输出流                 append参数: 默认不写是false,改为true可追加写入
        FileOutputStream out = new FileOutputStream("file1.txt",true);
        //方式一
//      out.write(97);
//      out.write(97);
//      out.write(97);
        //方式二
//      byte[] bytes = {12,23,45,56,63};
//      out.write(bytes);
        //方式三
        byte[] bs1 = "abcdefg".getBytes(StandardCharsets.UTF_8);
        out.write(bs1,0,3);//从索引off开始,写length个字节

        for (int i = 0; i < 5; i++) {
            out.write("hello".getBytes(StandardCharsets.UTF_8));
            //换行的三种方式
            out.write("\r\n".getBytes(StandardCharsets.UTF_8));
            //out.write("\n".getBytes(StandardCharsets.UTF_8));
                //out.write("\r".getBytes(StandardCharsets.UTF_8));
        }
            out.close();
    }

    @Test
    public void test03() throws Exception{
        /**
         * 字节流写数据加异常处理
         * 一般直接在外面包try{}catch{},
         * 但是会有一个问题,如果写入数据失败会运行catch的内容,
         * 但是close方法没有运行到也就是资源没有被释放。所以在io操作一定要保存内存被释放。
         *
         * 提供了finally块来执行所有清除操作
         * finally:在异常处理时提供finally块来执行所有清除的操作。比如io流中的资源释放。
         * 特点:被finally块控制的语句一定会执行,除非JVM退出
         * */
        //创建out输出流
        FileOutputStream out = null;       //append参数: 默认不写是false,改为true可追加写入
        try {
            out = new FileOutputStream("file1.txt", true);
            //方式一
            out.write(97);
            out.write(97);
            out.write(97);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void test04() throws Exception {
        /**
         * 读取一个字节
         * */
        File file = new File("file2.txt");
        //如果不存在 就创建
        if(!file.exists()){
            file.createNewFile();
        }
        //创建字节输入流
        FileInputStream in = new FileInputStream(file);
        //从这个输入流读取一个字节的数据。
        int read = in.read();
        System.out.println("read = " + read);
        in.close();
    }
    @Test
    public void test05() throws Exception {
        /**
         * 循环读取该文件全部内容
         * */
        File file = new File("file3.txt");
        //如果不存在 就创建
        if(!file.exists()){
            file.createNewFile();
        }
        //创建字节输入流
        FileInputStream in = new FileInputStream(file);
        //从这个输入流读取一个字节的数据。
        int read = in.read(); //int read ;
        while(read != -1){    //while((read = in.read) != -1)
            System.out.println((char)read);
            read = in.read(); //
        }
        in.close();
    }
    @Test
    public void test06() throws Exception {
        /**
         * 复制文本文件
         * */
        File file1 = new File("file2.txt");
        if(!file1.exists()){
            file1.createNewFile();
        }
        File file2 = new File("file3.txt");
        if(!file1.exists()){
            file1.createNewFile();
        }
        FileInputStream in = new FileInputStream(file1);
        FileOutputStream out = new FileOutputStream(file2);

        int bys; //实际写入的字节数
        //设置一个
        byte[] bytes = new byte[1024];
        while((bys = in.read(bytes)) != -1){
            out.write(bys);
        }
        out.close();
        in.close();
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dige____

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值