字节流复制文本

思路:
1.根据数据源创建字节输入流对象
2.根据目的地创建字节输出流对象
3.读写数据,复制文本文件(一次读一个字节,一次写一个字节)

public class CopyTxtDemo {
    public static void main(String[] args) throws IOException {
//        1.根据数据源创建字节输入流对象
        FileInputStream fis = new FileInputStream("C:\\Users\\PCTC\\Desktop\\file\\map.txt");

//        2.根据目的地创建字节输出流对象
        FileOutputStream fos = new FileOutputStream("C:\\Users\\PCTC\\Desktop\\file\\map1.txt");
//        3.读写数据,复制文本文件(一次读一个字节,一次写一个字节)
            int by ;
            while ((by = fis.read()) != -1){
                fos.write(by);
            }


            fis.close();
            fos.close();
    }
    public void add() throws IOException {
        FileInputStream fis =new FileInputStream("C:\\Users\\PCTC\\Desktop\\file\\map.txt");

//        2.调用字节输入流对象的读数据方法
//        int     read​() 从该输入流读取一个字节的数据。
//            int by = fis.read();
//            System.out.println(by);
//            System.out.println((char)by);

        //如果达到文本末尾 -1

//            while (by != -1){
//                System.out.print((char)by);
//                by  =fis.read();
//            }

        //优化
        int by;
        while ((by = fis.read()) != -1){
            System.out.print((char)by);
        }

//       3.释放资源
        fis.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值