IO流的基本使用方法

IO流主要用来做数据的传输,比如输入输出等操作,分为字节流和字符流。字符流主要处理一个文本文件的传输,字节流则什么类型的文件都可以。以下分类介绍这些流的使用方法:

  • 字节流
    字节流有两个基本的抽象(abstract)类:InputStream(输入流超类),OutputStream(输出流超类)
    普通字节流的文件操作:
//基本字节流,一次读取一个字节(效率很低)
static void copy(String src, String dest) throws IOException{
    FileInputStream  inputStream = new FileInputStream(src);

    FileOutputStream outputStream = new FileOutputStream(dest);

    //每次读取数据到by,当等于-1时文件读取完成
    int by = 0;
    while((by = inputStream.read())!= -1){
        outputStream.write(by);
    }

    outputStream.close();
    inputStream.close();
}
//基本字节流,一次读取一个字节数组
static void copy(String src, String dest) throws IOException {
    FileInputStream inputStream = new FileInputStream(src);

    FileOutputStream outputStream = new FileOutputStream(dest);

    //每次读取数据到bys字节数组,当等于-1时文件读取完成
    byte[] bys = new byte[1024];
    int length = 0;
    while ((length = inputStream.read(bys)) != -1) {
        outputStream.write(bys, 0, length);
    }

    outputStream.close();
    inputStream.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值