zoukankan      html  css  js  c++  java
  • 字节流核心代码

    public class demon3_copy {
      //  IO流核心代码
        public static void main(String[] args) throws IOException {
            FileInputStream fi1 = new FileInputStream("xxx.txt");
            FileOutputStream fo1 = new FileOutputStream("yyy.txt");
            int len;
            byte[] arr = new byte[2];
            while ((len = fi1.read(arr))!=-1) {
                fo1.write(arr,0,len);    //  len参数  代表写出有效个数
            }
            fo1.close();    //记得关流
            fi1.close();
        }
    
    }

    如果文件很大,一次写2个字节肯定很慢,如何一次将数据都读进来,再写出来:

      int len = f1.available()     :  读取文件的所有字节个数

      Byte[]  arr = new Byte[len]  :

      read(b);       一次全读出,  

      write(b); 一次全写出。

      这样写,Byte数组将会非常大,会造成内存溢出。

    解决方法:定义小数组 。

      Byte[] arr = new Byte[8*1024]       

    缓冲区读写方式 

      ### BufferedInputStream 内置了一个缓冲区(数组)
        * 从BufferedInputStream中读取一个字节时,BufferedInputStream会一次性从文件中读取8192个, 存在缓冲区中, 返回给程序一个b,程序再次读取时,
        就不用找文件了, 直接从缓冲区中获取,直到缓冲区中所有的都被使用过, 才重新从文件中读取8192个
        提高了速度

        BufferedOutputStream 也内置了一个缓冲区(数组)
        * 程序向流中写出字节时, 不会直接写到文件, 先写到缓冲区中
        * 直到缓冲区写满, BufferedOutputStream才会把缓冲区中的数据一次性写到文件里

        BufferedInputStream是 FileInputStream 的子类 BufferedOutputStream是 FileOutputStream的子类
        构造方法接收它们的父类,相当于把它们的父类包装了一下,比父类功能更强大 

    public class test1 {
        /* 
         * 图片加密   写出的时候   异或一个数,  读出的时候 再异或一个相同的数就可以正常读出了。   这个数就相当于密码
         */
        public static void main(String[] args) throws IOException {
            BufferedInputStream b1 = new BufferedInputStream(new FileInputStream("copy.jpg"));
            BufferedOutputStream b2 = new BufferedOutputStream(new FileOutputStream("copy2.jpg"));
            int b;
            while((b = b1.read()) != -1){
                b2.write(b ^ 1234);
            }
            b2.close();
            b1.close();
        }
    
    }
    public class test2 {
        /* 
         * 在控制台录入文件的路径,将文件拷贝到当前项目下
         */
        public static void main(String[] args) throws IOException {
            File file = getfile();
            BufferedInputStream b1 = new BufferedInputStream(new FileInputStream(file));
            BufferedOutputStream b2 = new BufferedOutputStream(new FileOutputStream(file.getName()));
            int b;
            while ((b = b1.read()) != -1) {
                b2.write(b);
            }
            b1.close();
            b2.close();
            
        }
        public static File getfile(){
            Scanner s1 = new Scanner(System.in);
            System.out.println("请输入文件路径");
            while (true) {
                String line = s1.nextLine();
                File f1 = new File(line);
                if (!f1.exists()) {
                    System.out.println("您输入的路径不存在,请重新录入");
                }else if (f1.isDirectory()) {
                    System.out.println("您输入的是文件夹,请重新录入");
                }else {
                    return f1;
                }
            }
        }
        
    
    }
    public class test3 {
        /*
         * 将键盘录入的数据拷贝到当前项目下的text.txt文件中,键盘录入数据当遇到quit时就退出
         */
        public static void main(String[] args) throws IOException {
            Scanner s1 = new Scanner(System.in);
            FileOutputStream fo1 = new FileOutputStream("Text.txt");
            while (true) {
                String line = s1.nextLine();
                if ("quit".equals(line)) {
                    break;
                }
                fo1.write(line.getBytes());
                fo1.write("
    ".getBytes());
            }
            s1.close();
            fo1.close();
        }
    
    }

    字节流读写中文,可能会出现乱码:

      因为 中文有时是三个字节,有时是2个字节

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class demon5_readzhongwen {
    
        public static void main(String[] args) throws IOException {
            demo1();
            FileOutputStream fo1 = new FileOutputStream("zzz.txt");
            fo1.write("我读书少,不要骗我".getBytes());
            fo1.write("
    ".getBytes());
            fo1.close();
        }
    
        public static void demo1() throws FileNotFoundException, IOException {
            FileInputStream fi1 = new FileInputStream("yyy.txt");
            byte[] arr = new byte[4];
            int len ;
            while((len = fi1.read(arr)) != -1){
                System.out.println(new String(arr,0,len));
            }
            fi1.close();
        }
    
    }
    竹杖芒鞋轻胜马,一蓑烟雨任平生。 回首向来萧瑟处,也无风雨也无晴。
  • 相关阅读:
    Android 基础-2.0 拔打电话号码
    Android 基础-1.0 按钮4种点击事件
    Android Studio 技巧备忘
    Android Studio Mac版快捷键
    face++静态库转为动态库之二
    Podfile语法参考
    iOS 高级去水印,涂鸦去水印
    vector 用法小例子
    UltraCompare 激活
    linux 日志查询
  • 原文地址:https://www.cnblogs.com/yaobiluo/p/11312403.html
Copyright © 2011-2022 走看看