IO的相关学习

IO的相关学习

1.IO和流基本了解

IO:Input:输入 Output:输出此技术用于处理设备之间的数据传输,能用于读写文件,网络通信等等。

在java中,IO的操作以流(stream)的方式进行 。

Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的

字符流的Reader,Writer 字节流的InputStream,OutputStream

流按照方向分为两种:输入流,输出流:

输入流指的是:往内存中存入数据,叫做输入。

输出流指的是:从内存中往外取数据,叫做输出。

按照读取数据的类型来分:

分为:字节流和字符流两种

什么是字节流:每次读取一个字节,一次操作8bit

什么是字符流:每次读取一个字符。比如"英雄联盟",读取的时候一次读取一个字符,一次操作16bit

区别:
字节流可以读取任何形式的数据:包括文本、音频、图像、视频等,而字符流只能读取纯文本形式的数据,也就是txt格式的数据。

InputStream 和 Reader 是所有输入流的基类

Writer和OutputStream是所有输出流的基类

Reader Writer是字符流的

InputStream OutputStream是字节流的

2.InputStream

InputStream抽象类

此抽象类是表示字节输入流的所有类的超类/抽象类,不可创建对象,其中使用最多的实现类是FileInputStream。

InputStream中常用方法:

abstract int read() 从输入流中读取数据的下一个字节

int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中

int read(byte[] b, int off, int len) 将输入流中最多 len 个数据字节读入 byte 数组,off表示存时的偏移量

void close() 关闭此输入流并释放与该流关联的所有系统资源

附:

FileInputStream的构造方法

通过打开与File类对象代表的实际文件的链接来创建FileInputStream流对象

若File类对象的所代表的文件不存在;不是文件是目录;或者其他原因不能打开的话,则会抛出FileNotFoundException

 public FileInputStream(File file) throws FileNotFoundException {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkRead(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        fd = new FileDescriptor();
        fd.attach(this);
        path = name;
        open(name);
    }

3.FileOutputStream

OutputStream抽象类

此抽象类是表示字节输出流的所有类的超类/抽象类,不可创建对象,其中使用最多的实现类是FileOutputStream。

此抽象类是表示输出字节流的所有类的超类.输出流接受输出字节并将这些字节发送到某个接收器.

FileOutputStream常用方法:

Void close() 关闭此输出流并释放与此流相关的所有系统资源

Void flush() 刷新此输出流并强制写出所有缓冲的输出字节

Void write(byte[ ] b) 将b.length个字节从指定的byte数组写入此输出流

Void write(byte[ ] b,int off ,int len) 将指定byte数组中从偏移量off开始的len个字节写入输出流

Abstract void write(int b) 将指定的字节写入此输出流

附:

FileOutputStream的构造方法

创建FileOutputStream流以写入数据到File对象所代表的文件,同时创建一个新的FileDescriptor对象来表示与该文件的关联(源码中会new一个该对象)

若文件存在,但是是目录而不是文件,则会抛出FileNotFoundException异常

public FileOutputStream(File file, boolean append)
        throws FileNotFoundException
    {
        String name = (file != null ? file.getPath() : null);
        SecurityManager security = System.getSecurityManager();
        if (security != null) {
            security.checkWrite(name);
        }
        if (name == null) {
            throw new NullPointerException();
        }
        if (file.isInvalid()) {
            throw new FileNotFoundException("Invalid file path");
        }
        this.fd = new FileDescriptor();
        fd.attach(this);
        this.append = append;
        this.path = name;

        open(name, append);
    }

4.流的操作

流的步骤:

1.确定源

2.打开流

3.操作流

4.关闭流

简单实例:

package com.Down;

import java.io.*;

public class Down {
    public void d1() {
        File ioinput = new File ("C:\\Users\\杨骜捷\\IdeaProjects\\Iouse\\src\\com\\Down\\ioinput.txt");
        File iooutput =new File("C:\\Users\\杨骜捷\\IdeaProjects\\Iouse\\src\\com\\Down\\outinput.txt");
        //确定源
        
        FileReader f1 = null;
        FileWriter f2 = null;
        try {
            
            f1 = new FileReader (ioinput);
            f2 = new FileWriter (iooutput);
            //打开流
            
            char[] chars = new char[100];
            int len;
            while ((len = f1.read(chars)) !=-1) {
            f2.write (chars, 0, len);
                System.out.println (chars);
            }
            ///操作流
        }catch (IOException e) {
            e.printStackTrace ();
        }catch (Exception exception){
            exception.printStackTrace ();
        }
        //捕获异常
        
        finally {
            //关闭流
        if(f1 != null) {
            try {
                f1.close ();
            }catch (IOException e){
                e.printStackTrace ();

            }finally {
                if (f2 != null) {
                    try {
                        f2.close ();
                    }catch (IOException e) {
                        e.printStackTrace ();
                    }
                }
            }
            }
        }

    }
}

结果:

请添加图片描述

QQ录屏20220525235332

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值