Java基础第16天 字节流

IO流 字节流

IO 流概述

程序需要实现与设备和不同介质之间的数据传输,例如:键盘录入、读取电脑文件等, Java 将这种通过不同输入输出设备(键盘,显示器,网络)等之间的数据传输抽象表述为“流”
按照操作的数据不同,可以分为:

  • 字节流:字节流可以操作任何数据,因为在计算机中任何数据都是以字节的形式存 储的
  • 字符流:字符流只能操作纯字符数据,比较方便。

按照流向分,又可以分为

输入流和输出流

字节流

字节输出流

字节输出流 OutputStream

OutputStream 是抽象类,是所有字节输出流类的超类。操作的数据都是字节,该类定 义了字节输出流的基本共性功能方法。

OutputStream 中定义的方法

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 类

OutputStream 有很多子类,其中子类 FileOutputStream 可用来写入数据到文件。 FileOutputStream 类,即文件输出流,是用于将数据写入 File 的输出流。
FileOutputStream 的构造方法,构造方法可以接受 File、String 类型的数据, append 为是否追加数据

构造方法摘要
FileOutputStream(File file)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(File file, boolean append)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(FileDescriptor fdObj)
创建一个向指定文件描述符处写入数据的输出文件流,该文件描述符表示一个到文件系统中的某个实际文件的现有连接。
FileOutputStream(String name)
创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(String name, boolean append)
创建一个向具有指定 name 的文件中写入数据的输出文件流。

输出的方法,close()方法可以关闭流,write 方法可以输出数据

方法摘要
void close()
关闭此文件输出流并释放与此流有关的所有系统资源。
protected void finalize()
清理到文件的连接,并确保在不再引用此文件输出流时调用此流的 close 方法。
void write(byte[] b)
将 b.length 个字节从指定 byte 数组写入此文件输出流中。
void write(byte[] b, int off, int len)
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
void write(int b)
将指定字节写入此文件输出流。

FileOutputStream 类输出数据到文件中

示例代码
public class Test02 {
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("C:\\aaa\\12.txt");
        FileOutputStream fos = new FileOutputStream(file);
        //fos.write("woxihuanni".getBytes());
        fos.write(100);//d
        byte[] b ={97,98,99,100};
        fos.write(b);//abcd
        fos.write(b, 2, 2);//cd;表示从数组索引2开始向后读取2个元素
        fos.close();

    }
}

字节输入流

字节输入流 InputStream

通过字节输出流,我们可以把内存中的数据写出到文件中,那如何想把文件中的数据读到内 存中,可以通过 InputStream 可以实现。
InputStream 是抽象类,是所有字节输入流类的超类,该类定义了字节输入流的基本共性功 能方法。

方法摘要

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

abstract int read()
从输入流中读取数据的下一个字节。
int read(byte[] b)
从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
int read(byte[] b, int off, int len)
将输入流中最多 len 个数据字节读入 byte 数组。

int read():读取一个字节并返回,没有字节返回-1.
int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字 节数。

FileInputStream 类

InputStream 有很多子类,其中子类 FileInputStream 可用来读取文件内容。
FileInputStream 从文件系统中的某个文件中获得输入字节。

构造方法摘要
FileInputStream(File file)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
FileInputStream(String name)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。

FileInputStream 类读取数据 read方法

在读取文件中的数据时,调用 read 方法,实现从文件中读取数据

int read()
从此输入流中读取一个数据字节。

从文件中读取数据,代码演示
public class Test03 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try (FileInputStream fis = new FileInputStream("C:\\aaa\\12.txt")) {
            // int ch = 0;
            // while((ch=fis.read())!=-1){
            // System.out.print((char)ch);
            // }
            int ch = 0;
            byte[] b = new byte[3];
            while ((ch = fis.read(b)) != -1) {

                for (int i = 0; i < ch; i++) {
                    System.out.print((char) b[i]);
                }
                System.out.println();
            }
        } catch (IOException e) {
            System.out.println("出错了");
        }
    }
}

FileInputStream 读取数据 read(byte[])方法

在读取文件中的数据时,调用 read 方法,每次只能读取一个,需要频繁的操作文件,效率 非常低,于是我们可以定义数组作为临时的存储容器,这时可以调用重载的 read 方法,一 次可以读取多个字符。这就好比从南京运烤鸭到淮安,如果有一万只烤鸭,那么需要运输一 万次,为了减少运输次数,可以先把一批烤鸭装载车厢中,这样可以成批的运送烤鸭了

代码示例
public class Test03 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try (FileInputStream fis = new FileInputStream("C:\\aaa\\12.txt")) {
            // int ch = 0;
            // while((ch=fis.read())!=-1){
            // System.out.print((char)ch);
            // }
            int ch = 0;
            byte[] b = new byte[3];
            while ((ch = fis.read(b)) != -1) {

                for (int i = 0; i < ch; i++) {
                    System.out.print((char) b[i]);
                }
                System.out.println();
            }
        } catch (IOException e) {
            System.out.println("出错了");
        }
    }
}

复制文件示例代码

public class Test04 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try(FileInputStream fis = new FileInputStream("C:\\aaa\\newnew12.txt");
                FileOutputStream fos = new FileOutputStream("C:\\aaa\\last12.txt")){
            int ch = 0;

            byte[] b = new byte[1];
            while((ch=fis.read(b))!=-1){
                fos.write(b, 0, ch);
            }
            fis.close();
            fos.close();

        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("出错了");
        }

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值