字节流相关操作,基本字节流的Copy文件

package OneStage.OneStageG.IObetyBuff;
/*
IO流:实质就是用来处理设备间传输数据的问题:
     文件的复制,文件上传,下载(硬盘与内存之间数据传输问题)
     输入:读数据,指把硬盘上的数据,读到内存中
     输出:写数据,指把内存中的数据,写到硬盘中
IO流的分类(根据数据类型来分):字节流,字符流
     字节流:字节输入流和字符输出流 (最底层的流,万用流)
     字符流:字符输入流和字符输出流 (主要是操作文本文件,用记事本打开可以用看懂的)
一. 字节流的相关操作
    字节流抽象基类:
    InputStream: 字节流的输入流抽象超类 eg:子类 FileInputStream
    outputStream:字节流的输出流抽象超类 eg:子类 FileOutputStream

 */

import java.io.*;

public class IODemoA {
    public static void main(String[] args) throws IOException {
        //定义一个文件路径
        String pathAndNane = "JavaOfBase\\src\\OneStage\\OneStageG\\IObetyBuff\\text.txt";
        //定义一个文件对象
        File test2File = new File("JavaOfBase\\src\\OneStage\\OneStageG\\IObetyBuff\\test2.txt");
        File test3File = new File("JavaOfBase\\src\\OneStage\\OneStageG\\IObetyBuff\\test3.txt");
        /*
        创建字节流输出对象,并关联到指定的文件,如果这个方法不存在,就按字符串内容创建文件
        FileOutputStream(String ss) 第一种构造方式 传字符串
        FileOutputStream(File ff) 第二种构造方式 传文件对象 (规范)
        FileOutputStream(test3File, true);第三种构造方法,让文件可以追加写入,参数值为:true
         */
        FileOutputStream testFOS = new FileOutputStream(pathAndNane);
        FileOutputStream test2FOS = new FileOutputStream(test2File);
        FileOutputStream test3FOS = new FileOutputStream(test3File, true);
        //调用write(int b ),write(byte[] b),write(byte[] int off,int len)方法写入数据
        testFOS.write(97);
        testFOS.write(98);
        byte[] b = {97,98,99,100,101};
        byte[] bys = "EFGHKLMN".getBytes();
        test2FOS.write(b);
        test2FOS.write(bys);
        test2FOS.write(bys,2,4);
        /*
        向文件写入数据的时候,换行的处理,不同的操作系统,对换行符的标识是不一样的
        windows:\r\n, Linux:\n, Mac:\r
         */
        test2FOS.write("\r\n".getBytes());
        test2FOS.write("我爱Java".getBytes());
        /*
        文件追加写入数据
        FileOutputStream(test3File, true);第三种构造方法,让文件可以追加写入,参数值为:true
         */
        test3FOS.write("我在学习Java".getBytes());
        test3FOS.write("\r\n".getBytes());
        //☆ 关闭释放资源 让与此流相关的操作全部断开,其它的程序才可以访问这些文件
        testFOS.close();
        test2FOS.close();
        test3FOS.close();

    }
}

 字节流,读取文件

 /*
字节流读取数据方法
 */
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class IODemoB {
    public static void main(String[] args) throws IOException {
    //声名读取文件的路径和名称
    String path = "JavaOfBase\\src\\OneStage\\OneStageG\\IObetyBuff\\readDemo.txt";
    //定义文件对象
        File pathAndName = new File(path);
        FileInputStream fileInp = new FileInputStream(pathAndName);
        /*
          read()返回为什么是int 因为是字节读取,所以底层是整数,一次只读一个字节
          当读取到没有字符的时候,返回-1,这样可以用来判断是否读到文件尾,可以做循环遍历
         */
        int readByte ;
        /*
        fileInp.read() 读取数据
        readByte=fileInp.read() 赋值操作
        对返回的值做了判断
         */
        while ((readByte=fileInp.read())!=-1){
            System.out.print((char)readByte);
        }
        //关闭字节文件读取流
        fileInp.close();

    }
}
-------一次读一个字节数组数据------
 public class ReadByByteArray {
    public static void main(String[] args) throws IOException {
        //定义一个文件路径
        String filePath = "JavaOfBase\\src\\OneStage\\OneStageG\\IObetyBuff\\text.txt";
        //生成文件对象
        File srcFile = new File(filePath);
        //构造一个读取文件流
        FileInputStream srcFileStream = new FileInputStream(srcFile);
        //定义一个接收的字节数组
        byte[] bysTemp = new byte[1024];
        /*
        /获得读取数据的长度
        // int len = srcFileStream.read(bysTemp);
        len:返回的是读取到的字节长度,当没读取到数据时,就返回-1,这个可以用来判断数据是否读完了
         */
        int len;
        while ((len =srcFileStream.read(bysTemp))!=-1){
            System.out.println(len);
            //调用String()中的方法,将字节转化成字符
            String contTemp = new String(bysTemp);
            System.out.print(contTemp);
        }
        //关闭流
        srcFileStream.close();
    }
}

-----字节缓冲流,读写文件-------

 

/*
字节缓冲输出流:ufferedOutputStream 基于字节流对象,创建一个缓冲区,将件多次写入缓冲区中,然后利用字节流对象调用底层对象方法写入字节,
             不必每个字节调用底层的方法,提高效率
 字节缓冲输入流:BufferedInputStream 基于字节流对象,创建一个缓冲区数组,一次性多读多个字节,读取效率高。
 */

public class BetyBuffStreamDemo {
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        //申明文件路径
        String srcPath = "JavaOfBase\\src\\OneStage\\OneStageG\\IObetyBuff\\jdk9.CHM";
        String decPath="JavaOfBase\\src\\OneStage\\OneStageF\\jdk9.CHM";
        //创建文件对象
        File srcFile = new File(srcPath);
        File decFile = new File(decPath);
        //创建文件流对象
        FileInputStream srcInputStream = new FileInputStream(srcFile);
        FileOutputStream decOutputStream = new FileOutputStream(decFile);
        //创建字节缓冲流对象
        BufferedInputStream srcBuffInputStream = new BufferedInputStream(srcInputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(decOutputStream);
        //定义一个数组大小 缓冲区大小
        byte[] bys = new byte[1024];
        //定义一个读写数据结尾的标记
        int flagLen;
        //边读数据,边写数据
        while ((flagLen=srcBuffInputStream.read(bys,0,bys.length))!=-1){
            decOutputStream.write(bys,0,bys.length);
        }
        //关闭字节缓冲流
        srcBuffInputStream.close();
        bufferedOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值