以byte[]类型复制文件 Copy a file with read(byte[] data) and write(byte[] data)
来源:互联网 发布:打印机数据错误怎么办 编辑:程序博客网 时间:2024/05/16 07:41
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class CopyFileByte {public static void main(String[] args) throws IOException {File file = new File("c:\\mm.txt");FileInputStream fis = null;FileOutputStream fos = null;fis = new FileInputStream(file);fos = new FileOutputStream("c:\\mm1.txt");byte[] buff = new byte[1024];int byteRead;/** * 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 * public void write(byte[] b, int off, int len) 参数: b - 数据。 off - 数据中的起始偏移量。 len - 要写入的字节数。 */while((byteRead = fis.read(buff)) > 0){fos.write(buff,0,byteRead);//以1024个字节数从0开始向buff中写入}fis.close();fos.close();}}