IO流

IO流的分类:
流向:输入流 输出流
单位: 字节流 字符流
字节输入流 字节输出流 字符输入流 字符输出流
InputStream OutputStream
使用字节输出流来实现写出数据;
FileOutputStream
在这里插入图片描述
换行写 追加写 异常处理
使用字节输入流来读数据:
FileInputStream
在这里插入图片描述
实现文件的复制

/*
    将D:\\IO\os\\窗里窗外.txt文档复制到当前module中
 */
public class FileCopy {
    public static void main(String[] args) throws IOException {
        // 实现读数据
        FileInputStream  fis = new FileInputStream("D:\\IO\\os\\窗里窗外.txt");
        // 实现写数据
        FileOutputStream  fos = new FileOutputStream("day_11_IODemo\\copy\\窗里窗外.txt");
        // 使用输入流来读数据 每次读取一个字节 读完之后  立即使用输出流将数据写出到目标文件
        int len;
        long begin = System.currentTimeMillis();// 获取开始 复制的时间
        while((len = fis.read()) != -1){
            fos.write(len);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制文件完成,共耗时:" + (end - begin) + "毫秒");//复制文件完成,共耗时:2189毫秒
        // 释放资源
        fos.close();
        fis.close();
    }
}


使用字节流读取数据(每次读取一个字节数组)

public class FileReadByteArray {
    public static void main(String[] args) throws IOException {
        FileInputStream  fis = new FileInputStream("D:\\IO\\os\\fos.txt");
      //  int read(byte[] b)
        byte[] buff = new byte[10];
        int len;
      
        while( (len =fis.read(buff)) != -1){//每次读取10个字节  在去输出
            // 将读取到的字节数组转换为字符串 输出
            System.out.println(new String(buff));
        }
        fis.close();
    }


在这里插入图片描述

\n w o r l d l l o \r
解决方法
String(byte[] bytes, int offset, int length)
通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。

public class FileReadByteArray {
    public static void main(String[] args) throws IOException {
        FileInputStream  fis = new FileInputStream("D:\\IO\\os\\fos.txt");
      //  int read(byte[] b)
        byte[] buff = new byte[10];
        int len;
        while( (len =fis.read(buff)) != -1){//每次读取10个字节  在去输出
            // 将读取到的字节数组转换为字符串 输出转换的时候 读几个就转换几个
            System.out.print(new String(buff,0,len));//
        }
        fis.close();
    }
}


使用每次读取一个字节数组的方式 实现文件的复制

/**
 * @author Mr.Hu
 * @create 2021/01/13
 *
 *     将D:\\IO\os\\窗里窗外.txt文档复制到当前module中  采用读取一个字节数组的方式
 *
 */
public class FileCopyByByteArray {
    public static void main(String[] args) throws IOException {
        FileInputStream  fis= new FileInputStream("d:\\IO\\os\\窗里窗外.txt");
        FileOutputStream fos = new FileOutputStream("day_11_IODemo\\copy\\copy.txt");
        byte[] buff = new byte[1024];
        int len;
        long begin= System.currentTimeMillis();
        while((len = fis.read(buff))!= -1){
            fos.write(buff,0,len);
        }
        long end = System.currentTimeMillis();
        System.out.println("复制文件完成,共耗时:" + (end - begin) + "毫秒");
        fos.close();
        fis.close();
    }


使用每次读取一个字节数组的方式 实现图片的复制

public static void main(String[] args) throws IOException {
    FileInputStream  fis = new FileInputStream("D:\\IO\\os\\fos.txt");
  //  int read(byte[] b)
    byte[] buff = new byte[10];
    int len;
    while( (len =fis.read(buff)) != -1){//每次读取10个字节  在去输出
        // 将读取到的字节数组转换为字符串 输出转换的时候 读几个就转换几个
        System.out.print(new String(buff,0,len));//
    }
    fis.close();
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值