Java流——字节流

字节流的使用


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


在这里插入图片描述

在这里插入图片描述

  • 文件字节输入流:FileInputStream
    • 作用:以内存为基准,把磁盘文件中的数据以字节的形式读取到内存中去。
构造器说明
public FileInputStream​(File file)创建字节输入流管道与源文件对象接通
public FileInputStream​(String pathname)创建字节输入流管道与源文件路径接通
方法名称说明
public int read()每次读取一个字节返回,如果返回-1,表示读取到了输入流的末尾
public int read(byte[] b)将数据读入一个字节数组,如果返回-1,表示读取到了输入流的末尾
public int read(byte[] b,int off,int len)将数据读入一个字节数组,off 指定在数组b 中存放数据的起始偏移位置,len 指定读取的最大字节数。如果返回-1,表示读取到了输入流的末尾。
  • 每次读取一个字节存在什么问题?
    • 性能较慢
    • 读取中文字符输出无法避免乱码问题。

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


在这里插入图片描述

  • 作用:以内存为基准,把磁盘文件中的数据以字节的形式读取到内存中去。
方法名称说明
public int read()每次读取一个字节返回,如果返回-1,表示读取到了输入流的末尾
public int read(byte[] b)将数据读入一个字节数组,如果返回-1,表示读取到了输入流的末尾
public int read(byte[] b,int off,int len)将数据读入一个字节数组,off 指定在数组b 中存放数据的起始偏移位置,len 指定读取的最大字节数。如果返回-1,表示读取到了输入流的末尾。
  • 每次读取一个字节数组存在什么问题?
    • 读取的性能得到了提升
    • 读取中文字符输出无法避免乱码问题。

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


  1. 如何使用字节输入流读取中文内容输出不乱码呢?

    • 定义一个与文件一样大的字节数组,一次性读取完文件的全部字节。
  2. 直接把文件数据全部读取到一个字节数组可以避免乱码,是否存在问题?

    • 如果文件过大,字节数组可能引起内存溢出。
  • 官方为字节输入流InputStream提供了如下API可以直接把文件的全部数据读取到一个字节数组中

  • public byte[] readAllBytes() throws IOException:直接将当前字节输入流对应的文件对象的字节数据装到一个字节数组返回

文件字节输出流:写字节数据到文件


  • 作用:以内存为基准,把内存中的数据以字节的形式写出到磁盘文件中去的流。
构造器说明
public FileOutputStream​(File file)创建字节输出流管道与源文件对象接通
public FileOutputStream​(File file,boolean append)创建字节输出流管道与源文件对象接通,可追加数据
public FileOutputStream​(String filepath)创建字节输出流管道与源文件路径接通
public FileOutputStream​(String filepath,boolean append)创建字节输出流管道与源文件路径接通,可追加数据
  • 文件字节输出流(FileOutputStream)写数据出去的API
方法名称说明
public void write(int a)写一个字节出去
public void write(byte[] buffer)写一个字节数组出去
public void write(byte[] buffer , int pos , int len)写一个字节数组的一部分出去。
  • 流的关闭与刷新
方法说明
flush()刷新流,还可以继续写数据
close()关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据
  • 字节输出流如何实现写出去的数据能换行

    • os.write(“\r\n”.getBytes())
  • 如何让写出去的数据能成功生效?

    • flush()刷新数据
    • close()方法是关闭流,关闭包含刷新,关闭后流不可以继续使用了。

文件拷贝


在这里插入图片描述

在这里插入图片描述

import java.io.*;  
//复制文件
public class FileCopy {  
    public static void main(String[] args) {  
        InputStream input = null;  
        OutputStream out = null;  
        try {  
            input = new FileInputStream("F:\\java练习\\LOL\\英雄联盟\\hero\\金克丝.txt");  
            out = new FileOutputStream("F:\\java练习\\LOL\\英雄联盟\\gold\\test.txt");  
            int len;  
            byte[] bytes = new byte[1024];  
            while ((len=input.read(bytes))>0) {  
                out.write(bytes,0,len);  
                //Thread.sleep(3000);//没循环一次停止3s  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        }  catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            //先开的后关闭  
            try {  
                if (out!=null) {  
                    out.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            try {  
                if (input!=null) {  
                    input.close();  
                }  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  
    }  
}
import java.io.*;  
//复制图片 
public class PicCopy {  
    public static void main(String[] args) {  
        InputStream input = null;  
        OutputStream out = null;  
        try {  
            input = new FileInputStream("D:\\picture\\恶灵.jpg");  
            out = new FileOutputStream("F:\\java练习\\LOL\\英雄联盟\\gold\\el.jpg");  
            byte[] b = new byte[1024];  
            int len;  
            while ((len = input.read(b))>0) {  
                out.write(b,0,len);  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }finally {  
                try {  
                    if (out!=null) {  
                        out.close();  
                    }  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
                try {  
                    if (input!=null) {  
                        input.close();  
                    }  
                } catch (IOException e) {  
                    e.printStackTrace();  
            }  
        }  
    }  
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值