Java-IO流(字节流)

在这里插入图片描述

IO流概述及其分类

  • IO流概述:
    IO流用来处理设备之间的数据传输
    Java对数据的操作是通过流的方式
    Java用于操作流的对象都在IO包中 java.io
  • IO流分类:
    (1):按照数据流向 站在内存角度
    输入流:读入数据
    输出流:写出数据
    (2):按照数据类型
    字节流:可以读写任何类型的文件 比如音频 视频 文本文件
    字符流:只能读写文本文件
    什么情况下使用哪种流呢?
    如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。
    如果你什么都不知道,就用字节流

IO流基类概述和FileOutputStream的构造方法

  • IO流基类概述:
    a:字节流的抽象基类:
    InputStream ,OutputStream。
    b:字符流的抽象基类:
    Reader , Writer。
    注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。
    如:InputStream的子类FileInputStream。
    如:Reader的子类FileReader。

  • FileOutputStream的构造方法
    FileOutputStream(File file)
    FileOutputStream(String name)

  Io流的分类:
  (1): 按照流向进行划分
  	输入流
  	输出流
  (2): 按照操作的数据类型进行划分
    字节流
    字节输入流	InputStream		读
    字节输出流	OutputStream	写
    字符流
    字符输入流 	Reader			读
    字符输出流	Writer			写

FileOutputStream写出数据

  • 案例演示
    FileOutputStream写出数据
public class MyTest2 {
    public static void main(String[] args) throws IOException {
        FileOutputStream out = new FileOutputStream("a.txt");
        out.write(97); 
        out.write(98);
        out.write(99);
        out.write(100);
        out.write(300);
        byte[] bytes = {101, 102, 103, 104};
        out.write(bytes);
        out.write(bytes, 2, 2);
        String str = "邻家有女初长成,养在深闺人未识";
        byte[] bytes1 = str.getBytes();
        out.write(bytes1);
        out.close();
    }
}
  • 注意事项:
    创建字节输出流对象了做了几件事情?
    a:调用系统资源创建a.txt文件
    b:创建了一个fos对象
    c:把fos对象指向这个文件
    为什么一定要close()?
    a: 通知系统释放关于管理a.txt文件的资源
    b: 让Io流对象变成垃圾,等待垃圾回收器对其回收

FileOutputStream的三个write()方法

  • FileOutputStream的三个write()方法
    • public void write(int b):写一个字节 超过一个字节 砍掉前面的字节
    • public void write(byte[] b):写一个字节数组
    • public void write(byte[] b,int off,int len):写一个字节数组的一部分

FileOutputStream写出数据实现换行和追加写入

	FileOutputStream写出数据如何实现数据的换行

		 windows下的换行符只用是 \r\n
		  Linux		\n
 		  Mac		\r

 eclipse/IDEA 中的记事本软件以及editplus这样的第三种软件都做了平台的兼容

FileInputStream读取数据一次一个字节

  • 案例演示: int read():一次读取一个字节
    如果没有数据返回的就是-1
public class Demo3 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream(new File("D:\\video01.avi"));
        FileOutputStream out = new FileOutputStream(new File("D:\\video02.avi"));
        int len=0;
        System.out.println("基本字节流一次读一个字节");
        while((len=in.read())!=-1){
            out.write(len);
        }
    }
}

FileInputStream读取数据一次一个字节数组

  • 案例演示: int read(byte[] b):一次读取一个字节数组
    返回的int类型的值表示的意思是读取到的字节的个数,如果没有数据了就返回-1
public class Demo3 {
    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream(new File("D:\\video01.avi"));
        FileOutputStream out = new FileOutputStream(new File("D:\\video02.avi"));
        int len=0;
        byte[] bytes = new byte[1024 * 8];
        System.out.println("基本字节流一次读一个字节数组");
        while((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
    }
}

BufferedInputStream一次读取一个字节

  • 缓冲思想
    字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,
    这是加入了数组这样的缓冲区效果,java本身在设计的时候,
    也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流*
  • BufferedOutputStream的构造方法
    查看API
    BufferedOutputStream(OutputStream out)
  • 案例演示
    BufferedInputstream读入数据
    BufferedOutputStream写出数据
public class Demo3 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:\\video01.avi"));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\video02.avi"));
        int len=0;
        while ((len=in.read())!=-1){
            out.write(len);
        }
    }
}

BufferedInputStream一次读取一个数组

  • 案例演示
public class Demo3 {
    public static void main(String[] args) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:\\video01.avi"));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\video02.avi"));
        int len=0;
        byte[] bytes = new byte[1024 * 8];
        while ((len=in.read(bytes))!=-1){
            out.write(bytes,0,len);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值