JAVA零基础之IO流详解(二)——字节流

IO流:输入输出流


在这里插入图片描述

字节流
  • 字节输入流
import java.io.FileInputStream;
import java.io.IOException;

public class IODemo01 {
    public static void main(String[] args) {
        try {
            /*
            铺设管道的方法一
            File f1 = new File("D:\\20200122.txt");
            if(f1.exit() && f1.length()>0 ) {
                FileInputStream fis = new FileInputStream(f1);
             */
            // 铺设管道的方法二
            FileInputStream fis = new FileInputStream("D:\\20200122.txt");
            int ch = 0;
            while ((ch = fis.read()) != -1) {
                System.out.println((char)ch);
            }

            fis.close();
        } catch (IOException e) {
            System.out.println("文件找不到");
        }
    }
}

  • 字节输出流
import java.io.FileOutputStream;

public class IODemo02 {
    public static void main(String[] args) {
        try {
            // 1.创建水厂
            String data = "hello html, hello shenyishan";
            // 2. 铺设程序通往盘符的管道
            FileOutputStream fos = new FileOutputStream("D:\\20210122.txt", true);
            // 3. 开水龙头,防水
            byte[] tempByte = data.getBytes();
            //如果文件不存在,则会自动帮我们创建文件
            fos.write(tempByte);
            // 4. 关水龙头
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • IO流实现复制
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class IODemo03 {
    public static void main(String[] args) {
        try {
            long startTime = System.currentTimeMillis();
            //1.在文件和程序之间铺设管道
            FileInputStream fis = new FileInputStream("D:\\20200122.txt");
            //创建一个程序通往盘符的管道
            FileOutputStream fos = new FileOutputStream("D:\\20190225.txt");
            //2. 开水龙头
            int ch = 0;
            while ((ch=fis.read()) != -1) {
                fos.write(ch);
            }
            // 3.关水龙头
            fis.close();
            fos.close();
            long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime + "ms");
        } catch (IOException e) {
            System.out.println("文件找不到");
        }
    }
}

}

  • 粗管道
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class IODemo04 {
    public static void main(String[] args) {
        try {
            long startTime = System.currentTimeMillis();
            //1. 水厂(文件)
            //2. 铺设管道
            InputStream fis = new FileInputStream("D:\\20200122.txt");
            BufferedInputStream bis = new BufferedInputStream(fis);
            //3. 开水龙头
            //创建一辆小车
            byte[] car = new byte[1024];
            int len = 0;
            while ((len=bis.read(car)) != -1) {
                System.out.println(len);
            }
            bis.close();
            long endTime = System.currentTimeMillis();
            System.out.println("共消耗" + (endTime-startTime)+ "ms");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值