JAVA IO 流 字节流 字符流 换行符 try catch 的Finally 字段

学习IO流的目的:

将数据写到文件中,实现数据永久化存储

 读取文件中已经存在的数据

I表示input 是数据从硬盘进内存的过程称为读 

O表示output  是数据从内存到硬盘的过程 称之为写 

其实就是 内存在读 内存在写

IO流的分类

 

纯文本文件 需要用记事本打开读的懂 。 office文件都不算

字节流  字节输出

构造方法

    • FileOutputStream​(File file)

      创建文件输出流以写入由指定的 File对象表示的文件。

      FileOutputStream​(FileDescriptor fdObj)

      创建要写入指定文件描述符的文件输出流,该文件描述符表示与文件系统中实际文件的现有连接。

      FileOutputStream​(File file, boolean append)

      创建文件输出流以写入由指定的 File对象表示的文件。

      FileOutputStream​(String name)

      创建文件输出流以写入具有指定名称的文件。

      FileOutputStream​(String name, boolean append)

      创建文件输出流以写入具有指定名称的文件。

第三种是:write(数组名,开始索引,写的个数);

字节流写数据的三种方式

void write​(int b)

一次写一个字节数据

void write​(byte[] b)

一次写一个字节数组数据

void write​(byte[] b, int off, int len)

一次写一个字节数组的部分数据 

换行符 :

windows:\r\n

linux:\n

mac:\r方法

由于字节流参数只能是byte类型 所以我们要输入字符串的"\r\n" 然后调用.getBytes()方法

如:fos.write("\r\n".getBytes());

字节输出实例

public static void main(String[] args) throws IOException {
    //1.创建对象字节输出流的对象---告诉虚拟机往那个文件中写数据
//注意 如果文件不存在,会帮我们自动创建出来 ,如果文件存在 会把文件清空
    FileOutputStream fos = new FileOutputStream("\\.a.txt");
    //2.写数据  传递整数时,那么实际写到文件中的是这个整数在码表中对应的那个字符
    fos.write(65); 
    //换行  
    fos.write("\r\n".getBytes());
    fos.write(66);
    //3.释放资源  告诉操作系统 我现在不需要再用这个文件了
    fos.close();
}

如果想续写文件,不清空内容,在构造该对象的时候设置第二个参数为true就可以了,默认是false

Finally  代表一定要运行的代码段

在字节流输出数据的时候 需要抛出异常,但是我们必须要执行close() 释放资源 所以我们需要finally

public static void main(String[] args) {
    //先创建空对象,以免最下的fos.close()编译报错
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream("test\\a.txt");
        //2.写数据
        fos.write(65);
        fos.write("\r\n".getBytes());
        fos.write(66);
    }catch (IOException e){
        e.printStackTrace();
    }finally {
        //fos.close() 是必须操作的语句 我们放在finally中
        // 先判断对象是否为空  避免空指针异常 ;再进行异常抛出操作
        if(fos!=null){try {
            fos.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        }

    }

字节输入流 

构造方法:

FileInputStream fis = new FileInputStream("text\\a.txt")

如果文件不存在 会直接报错 

int read= fis.read()

// 一次读取一个字节,返回值是读到的那个字节数据,也就是字符在码表中对应的数字

//如果想要看到的是字符数据,那么就用强转 

fis.close(); 释放资源

依次输入多个字节

FileInputStream fis = new FileInputStream("test\\a.txt");
int a;
//当read()读不到内容的时候返回值就是-1;
while ((a=fis.read())!=-1){
    //输入原来的字节,强转回char类型
    System.out.println((char)a);
}

单次读入批量的字节:

一次读一个字节数组的方法: public int read​(byte[] b):从输入流读取最多b.length个字节的数据到b数组中

返回的是读入缓冲区的总字节数,也就是实际的读取字节个数

如果该次文件到达末尾而,数组没有数据读入,则返还-1

输入输出流应用:文件复制

public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream("test\\a.txt");
    FileOutputStream fos = new FileOutputStream("test\\b.txt");
    int a;
    while ((a=fis.read())!=-1){
        //将遍历读的每个字节 写进新的文件里
        fos.write(a);
    }
    fis.close();
    fos.close();
}

提高文件复制效率:

public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream("test\\a.txt");
    FileOutputStream fos = new FileOutputStream("test\\b.txt");
    //创建个长度为1024的空数组容器
    byte[] b = new byte[1024];
    int len;
    //如果上次读完了全部,新数组没有东西存入 则会返还-1
    while ((len=fis.read(b))!=-1){
        //读到几个字节就写几个字节
        fos.write(b,0,len);

    };
    fis.close();
    fos.close();
}

字节流缓冲流

构造方法 :

输出:BufferedOutputStream(OutputStream out)

输入:BufferedInputStream(InputStream in)

缓冲流的输入跟输出默认会有一个长度为8192的数组

 减少了硬盘跟内存之间数据传递的次数,从而提高了性能

缓冲流结合数组,进行文件拷贝

public static void main(String[] args) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test\\a.txt"));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("test\\b.txt"));
    byte[] bytes =new byte[1024];
    int len ;
    while ((len=bis.read(bytes))!=-1){
        bos.write(bytes,0,len);
    }
    bis.close();
    bos.close();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值