• 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:

  • 相关阅读:
    wait/sleep/yield的区别
    54点提高PHP编程效率(转)
    ftp 使用
    转义 html 里特殊含义字符
    查看端口是否被占用
    webservice 获取调用者IP
    字符编码
    你有读过软件的协议或是网站的服务条款吗?
    使用TSQL的Rand函数生成随机数的艰苦历程
    几个.NET方面的问题——参考答案
  • 原文地址:https://www.cnblogs.com/lxr521/p/10602079.html
Copyright © 2020-2023  润新知