Java-字节输入输出。(新手用于记录每天的作业)
参考手册:
BufferedInputStream
BufferedOutputStream
实例:
import java.io.*; /* * 文件的复制方式 * 1 字节流读写单个字节 * 2 字节流读写字节数组 * 3 字节流缓冲区 读写单个字节 * 4 字节流缓冲区读写字节数组 */ public class Zjsrsclx { public static void main(String[] args) throws Exception { long e = System.currentTimeMillis(); File src = new File("D:\\ja.txt"); File dasc = new File("C:\\ja.txt"); long c = System.currentTimeMillis(); System.out.println(e-c); lx4(src,dasc); } private static void lx4(File src, File dasc) throws Exception { // 4 字节流缓冲区读写字节数组 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dasc)); byte[] b = new byte[1024*10]; int len = 0; while((len = bis.read(b))!=-1){ bos.write(b,0,len); } bis.close(); bos.close(); } private static void lx3(File src, File dasc) throws Exception { //3 字节流缓冲区 读写单个字节 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dasc)); int len = 0; while((len = bis.read())!=-1){ bos.write(len); } bis.close(); bos.close(); } private static void lx2(File src, File dasc) throws Exception { //2 字节流读写字节数组 FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dasc); int len = 0; byte[] b = new byte[1024*10]; while ((len = fis.read(b))!=-1){ fos.write(b,0,len); } fis.close(); fos.close(); } private static void lx1(File src,File dasc) throws Exception { //1 字节流读写单个字节 //复制粘贴文件。 FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dasc); int len = 0; while ((len = fis.read())!=-1){ fos.write(len); } fis.close(); fos.close(); } }
打印结果:
lx1:
lx2:
lx3:
lx4: