Java的IO流-字节流

 

 文件字节输入流:每次读取一个字节

package com.itheima.IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class Test1 {
    public static void main(String[] args) throws Exception {
        //1、创建文件字节输入流管道,与原文件接通
        //InputStream is = new FileInputStream(new File("bilibili\\src\\itheima01.txt"));
        //简化写法
        InputStream is = new FileInputStream("bilibili\\src\\itheima01.txt");

        //2、开始读取文件的字节数据
        //public int read():每次读取一个字节返回,如果没有数据了,返回-1.
        int b1 = is.read();
        System.out.println(b1);
        System.out.println((char) b1);

        int b2 = is.read();
        System.out.println(b2);
        System.out.println((char) b2);

        int b3 = is.read();
        System.out.println(b3);
        System.out.println((char) b3);

        int b4 = is.read();
        System.out.println(b4);
    }
}

package com.itheima.IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class Test1 {
    public static void main(String[] args) throws Exception {
        //1、创建文件字节输入流管道,与原文件接通
        //InputStream is = new FileInputStream(new File("bilibili\\src\\itheima01.txt"));
        //简化写法
        InputStream is = new FileInputStream("bilibili\\src\\itheima01.txt");

        //2、开始读取文件的字节数据
        //public int read():每次读取一个字节返回,如果没有数据了,返回-1.
//        int b1 = is.read();
//        System.out.println(b1);
//        System.out.println((char) b1);
//
//        int b2 = is.read();
//        System.out.println(b2);
//       System.out.println((char) b2);
//
//        int b3 = is.read();
//        System.out.println(b3);
//        System.out.println((char) b3);
//
//        int b4 = is.read();
//        System.out.println(b4);

        //使用循环读取
        int b;//用于记住读取的字节
        while ((b = is.read() )!= -1){
            System.out.print((char) b);
        }
        //读取数据的性能差
        //读取汉字输出会乱码,且无法避免
        //流使用完毕之后,必须关闭,释放系统资源
        is.close();
    }
}

 字节输入流:每次读取多个字节

package com.itheima.IO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class Test2 {
    public static void main(String[] args) throws Exception {
        //创建一个字节输入流对象代表字节输入流管道与源文件接通
        InputStream is = new FileInputStream("bilibili\\src\\itheima02.txt");

        //开始读取文件中的字节数据,每次读取多个字节。
        //public int read(byte b[]) throws IOException
        //每次读取多个字节到数组中去,返回读取的字节数量,读取完毕会返回-1
        byte[] buffer = new byte[3];
        int len = is.read(buffer);
        String rs = new String(buffer);//解码
        System.out.println(rs);
        System.out.println("当次读取的字节数量" + len);

        //注意:读取多少,到处多少
        int len2 = is.read(buffer);
        String rs2 = new String(buffer, 0, len2);
        System.out.println(rs2);
        System.out.println("当次读取的字节数量" + len2);

        int len3 = is.read(buffer);
        System.out.println(len3);
    }
}

package com.itheima.IO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class Test2 {
    public static void main(String[] args) throws Exception {
        //创建一个字节输入流对象代表字节输入流管道与源文件接通
        InputStream is = new FileInputStream("bilibili\\src\\itheima02.txt");

        //开始读取文件中的字节数据,每次读取多个字节。
        //public int read(byte b[]) throws IOException
        //每次读取多个字节到数组中去,返回读取的字节数量,读取完毕会返回-1
//        byte[] buffer = new byte[3];
//        int len = is.read(buffer);
//        String rs = new String(buffer);//解码
//        System.out.println(rs);
//        System.out.println("当次读取的字节数量" + len);
//
//        //注意:读取多少,到处多少
//        int len2 = is.read(buffer);
//        String rs2 = new String(buffer, 0, len2);
//        System.out.println(rs2);
//        System.out.println("当次读取的字节数量" + len2);
//
//        int len3 = is.read(buffer);
//        System.out.println(len3);
        byte[] buffer = new byte[3];
        int len;
        while ((len = is.read(buffer)) != -1){
            String rs = new String(buffer, 0, len);
            System.out.print(rs);
        }

        //读取汉字输出乱码
        is.close();
    }
}

  字节输入流:一次读取完全部字节

package com.itheima.IO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

public class Test3 {
    public static void main(String[] args) throws Exception {
        //一次性读取完文件的全部字节到一个字节数组中去
        InputStream is = new FileInputStream("bilibili\\src\\itheima03.txt");

        //2、准备一个字节数组,大小与文件的大小正好一样大
        File f = new File("bilibili\\src\\itheima03.txt");
        long size = f.length();
        byte[] buffer = new byte[(int) size];

        int len = is.read(buffer);
        System.out.println(new String(buffer));
        System.out.println(size);
        System.out.println(len);
    }
}

 

 注意:此方法JDK8没法用

byte[] buffer = is.readAllBytes();
System.out.println(new String(buffer));

 

文件字节输出流:写字节出去  

package com.itheima.IO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

public class Test4 {
    public static void main(String[] args) throws Exception {
        //创建一个字节输出流管道与目标文件接通
        //覆盖管道:覆盖之前的数据
//       OutputStream os = new FileOutputStream("bilibili/src/itheima04out.txt");

        //追加数据的管道
        OutputStream os = new FileOutputStream("bilibili/src/itheima04out.txt", true);
       //2、开始写字节数据出去了
        os.write(97);//97戴白哦一个字节a
        os.write('b');

        byte[] bytes = "sdvbdsbvdsvhsdk".getBytes(StandardCharsets.UTF_8);
        os.write(bytes);

        os.write(bytes,0, 4);

        os.write("\r\n".getBytes(StandardCharsets.UTF_8));

        os.close();
    }
}

 案例:文件复制

package com.itheima.IO;

import java.io.*;

public class Test5 {
    public static void main(String[] args) throws Exception {
        //1、创建一个字节输入管道与源文件接通
        InputStream is = new FileInputStream("C:\\Users\\RCH\\Pictures\\Screenshots");
        //2、创建一个字节输出流管道与目标文件接通
        OutputStream os = new FileOutputStream("D:\\z\\Screenshots");
        //3、创建一个字节数组,负责转移字节数据
        byte[] buffer = new byte[1024];
        //4、从字节输入流中读取字节数据,写出去到字节输出流中,读多少写出去多少
        int len;//记住每次读取了多少个字节
        while ((len = is.read(buffer)) != -1){
            os.write(buffer,0,len);
        }
        os.close();
        is.close();
        System.out.println("复制完成");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值