JavaSE(六):字节流操作

1、字节输出流

package com.wedu.io;

import java.io.FileOutputStream;
import java.io.IOException;
/**
 * @description 文件复制
 * @version JavaSE V1.0
 * @date 2019-09-21 17:06:47
 */
public class OutputStreamDemo {
	public static void main(String[] args) throws IOException {
		//1.创建一个FileOutputStream对象,构造方法中传递写入数据的目的地
        FileOutputStream fos = new FileOutputStream("out.txt");
        
        //2.调用FileOutputStream对象中的方法write,把数据写入到文件中
        writeSingle(fos);

        writeMultiple(fos);

        writePart(fos);

        sequenceWrite();
        
        //3.释放资源(流使用会占用一定的内存,使用完毕要把内存清空,提供程序的效率)
        fos.close();
	}
	
    /**
     * FileOutputStream(File file, boolean append)
     *      创建一个向指定 File 对象表示的文件中写入数据的文件输出流
     */
    private static void sequenceWrite() throws IOException {
        FileOutputStream fos = new FileOutputStream("out.txt",true) ;
        fos.write("123".getBytes());
        fos.close();
    }

    /**
     * public void write(byte[] b, int off, int len) throws IOException
     *      将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
     * @param fos
     */
    private static void writePart(FileOutputStream fos) throws IOException {
        byte[] bytes = {65, 66, 67, 68};
        fos.write(bytes,1,2);
    }

    /**
     * public void write(byte[] b) throws IOException
     * 将 b.length 个字节从指定 byte 数组写入此文件输出流中。
     * @param fos
     * @throws IOException
     */
    private static void writeMultiple(FileOutputStream fos) throws IOException {
        byte[] bytes = {65, 66, 67, 68};
        fos.write(bytes);
    }

    /**
     * public void write(int b) throws IOException
     * 将指定字节写入此文件输出流。实现 OutputStream 的 write 方法。
     * @param fos 文件输入流
     * @throws IOException
     */
    private static void writeSingle(FileOutputStream fos) throws IOException {
        fos.write(97);
    }
}

2、字节输入流

package com.wedu.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * @description 文件输入流操作
 * @version JavaSE V1.0
 * @date 2019-09-21 17:06:16
 */
public class InputStreamDemo {
    public static void main(String[] args) throws IOException {
        readSingle();
        readMultiple();
    }	

	/**
     *  int read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。 
     * @param fis
     * @throws IOException
     */
    private static void readMultiple() throws IOException {
    	FileInputStream fis = new FileInputStream("out.txt");
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = fis.read(bytes)) != -1) {
            System.out.println(new String(bytes,0,len));
        }
        fis.close();
    }

    /**
     *  int read() 从此输入流中读取一个数据字节。
     * @param fis
     * @throws IOException
     */
    private static void readSingle() throws IOException {
    	//1.创建FileInputStream对象,构造方法中绑定要读取的数据源
    	FileInputStream fis = new FileInputStream("out.txt");
        int len = 0;
        //2.使用FileInputStream对象中的方法read,读取文件
        while((len = fis.read()) != -1) {
            System.out.println(len);
        }
        //3.释放资源
        fis.close();
    }
}

3、文件复制

package com.wedu.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
 * @description 文件复制
 * @version JavaSE V1.0
 * @date 2019-09-21 17:06:16
 */
public class FileCopy {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("out.txt");
        FileOutputStream fos = new FileOutputStream("copy.txt");
        
    	copySingle(fis,fos);
    	
    	copyMultiple(fis,fos);
    	
        fos.close();
        fis.close();
	}

    /**
     * @description 多个字符复制
     * @param fis
     * @param fos
     * @throws IOException
     */
	private static void copyMultiple(FileInputStream fis,FileOutputStream fos) throws IOException {
        long start = System.currentTimeMillis();
        byte[] bytes = new byte[1024];
        int len = 0;
        while((len = fis.read(bytes)) != -1){
            fos.write(bytes,0,len);
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start));
	}
	
	/**
	 * @description 逐个字符复制
	 * @param fis
	 * @param fos
	 * @throws IOException
	 */
	private static void copySingle(FileInputStream fis,FileOutputStream fos) throws IOException {
        long start = System.currentTimeMillis();
        int len = 0;
        while((len = fis.read()) != -1) {
            fos.write(len);
        }
        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、付费专栏及课程。

余额充值