zoukankan      html  css  js  c++  java
  • JAVA笔记12__字节、字符缓冲流/打印流/对象流/

    /**
     * !!:以后写流的时候一定要加入缓冲!!
     * 对文件或其它目标频繁的读写操作,效率低,性能差。
     * 缓冲流:好处是能更高效地读写信息,原理是将数据先缓冲起来,然后一起写入或读取出来。
     * 
     * BufferedInputStream:字节缓冲流(有一个内部缓冲区数组,用于缓冲数据)
     */
    public class Main {
        public static void main(String[] args) {
            input();
            output();
        }
        
        /**
         * 使用字节缓冲流进行读取操作
         */
        public static void input(){
            try {
                InputStream in = new FileInputStream("c:\fuck\javaTest1.txt");
                BufferedInputStream bis = new BufferedInputStream(in); //根据字节输入流构造一个字节缓冲流
                Reader r= new InputStreamReader(bis);
                char[] cs = new char[512];
                int len = -1;
                StringBuilder sb = new StringBuilder();
                while((len=r.read(cs))!=-1){
                    sb.append(new String(cs));
                }
                r.close();
                bis.close();
                in.close();
                System.out.println(sb);
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        /**
         * 使用字节缓冲流进行写入操作
         */
        public static void output(){
            try {
                OutputStream out = new FileOutputStream("c:\fuck\javaTest1.txt");
                BufferedOutputStream bis = new BufferedOutputStream(out); //根据字节输出流构造一个字节缓冲流
                String info = "顶替要夺kasdfas山不在高";
                bis.write(info.getBytes()); 
                bis.flush(); //刷新缓冲区(把数据写入文件里)
                bis.close(); //关闭了会刷新缓冲区
                out.close();
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    public class Main {
        public static void main(String[] args) {
    //        output();
            input();
            System.out.println("finished.");
        }
        
        /**
         * 使用字符缓冲流进行读取操作
         */
        public static void input(){
            try {
                Reader r = new FileReader("c:\fuck\javaTest1.txt");
                BufferedReader br = new BufferedReader(r); //根据字符输入流构造一个字符缓冲流
                char[] cs = new char[512];
                int len = -1;
                StringBuilder sb = new StringBuilder();
                while((len=br.read(cs))!=-1){
                    sb.append(new String(cs));
                }
                br.close();
                r.close();
                System.out.println(sb);
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        /**
         * 使用字符缓冲流进行写入操作
         */
        public static void output(){
            try {
                Writer w = new FileWriter("c:\fuck\javaTest1.txt");
                BufferedWriter bw = new BufferedWriter(w); //根据字符输出流构造一个字符缓冲流
                bw.write("abcde i love u 丁");
                bw.flush();
                w.close();
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    /**
     * 打印流:主要功能是用于输出
     * PrintStream:字节打印流
     * PrintWriter:字符打印流
     */
    public class Main {
        public static void main(String[] args) {
    //        print();
            print2();
            System.out.println("finished.");
        }
        
        /**
         * 使用PrintStream打印流
         */
        public static void print(){
            try {
                OutputStream out = new FileOutputStream("c:\fuck\javaTest1.txt");
                BufferedOutputStream bos = new BufferedOutputStream(out);
                PrintStream ps = new PrintStream(bos); //构造字节打印流
                ps.println(3.14f);
                ps.println(188);
                ps.println(true);
                ps.println("it has.");
                ps.flush();
                bos.close(); //关掉一个就行了,其它也跟着关了
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        /**
         * 使用PrintWriter打印流
         */
        public static void print2(){
            try {
                BufferedWriter bw = new BufferedWriter(new FileWriter("c:\fuck\javaTest1.txt"));
                PrintWriter pw = new PrintWriter(bw);
                pw.print("
    "); //输出回车换行符
                pw.println(105);
                pw.println("饥饿营销"饿"成笑话");
                pw.flush();
                pw.close();
                bw.close(); 
                
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    /**
     * 对象流
     * 对象序列化:将一个对象转化成二进制的byte流
     * 序列化操作:将对象保存在文件上    ObjectOutputStream
     * 反序列化操作:将对象从文件中恢复     ObjectInputStream
     * 被序列化对象所在的类必须实现java.Serializable接口
     * 序列化一组对象可采用:对象数组的形式,因为对象数组可以向Object进行转型操作
     * transient关键字:声明一个实例变量,当对象存储时,它的值不需要维持。
     *      当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机制来保存它。
     *      为了在一个特定对象的一个域上关闭serialization,可以在这个域前加上关键字transient。
     *      当一个对象被序列化的时候,transient型变量的值不包括在序列化的表示中,然而非transient型的变量是被包括进去的。
     */
    public class Main {
        public static void main(String[] args) {
    //        writerObject();
    //        readerObject();
    //        writerObject2();
            readerObject2();
            System.out.println("finished.");
        }
        
        //使用ObjectOutputStream把对象写入文件
        public static void writerObject(){
            try {
                OutputStream out = new FileOutputStream("c:\fuck\obj.tmp");
                ObjectOutputStream oos = new ObjectOutputStream(out); //根据字节输出流构造一个对象流
                oos.writeInt(106);
                Dog d1 = new Dog("fish7",15);
                oos.writeObject(d1);
                oos.close();
                out.close();
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        //使用ObjectOutputStream把一组对象写入文件(对象数组)
        public static void writerObject2(){
            try {
                OutputStream out = new FileOutputStream("c:\fuck\obj.tmp");
                ObjectOutputStream oos = new ObjectOutputStream(out); //根据字节输出流构造一个对象流
                Dog[] dogs = {new Dog("fish7",15),new Dog("fish8",16),new Dog("fish9",17)};
                oos.writeObject(dogs);
                oos.close();
                out.close();
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        
        //使用ObjectInputStream读取文件中的对象
        public static void readerObject(){
            try {
                InputStream in = new FileInputStream("c:\fuck\obj.tmp");
                ObjectInputStream ois = new ObjectInputStream(in); //根据字节输入流构造一个对象流
                int num = ois.readInt();
                Dog d1 = (Dog)ois.readObject();
                ois.close();
                in.close();
                System.out.println(num);
                System.out.println(d1.name+" "+d1.age);
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
        }
        
        //使用ObjectInputStream读取文件中的一组对象(对象数组)
        public static void readerObject2(){
            try {
                InputStream in = new FileInputStream("c:\fuck\obj.tmp");
                ObjectInputStream ois = new ObjectInputStream(in); //根据字节输入流构造一个对象流
                Dog[] dogs = (Dog[])ois.readObject();
                ois.close();
                in.close();
                for(Dog i:dogs)
                    System.out.println(i.name+" "+i.age);
                
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    /**
     * 类通过实现java.Serializable接口以启用其序列化功能,标记接口,没有任何方法
     */
    class Dog implements Serializable{
        String name;
        transient int  age; //使用transient关键字声明的属性,将不会被序列化
    
        public Dog(String name, int age) {
            this.name = name;
            this.age = age;
        }
    }
  • 相关阅读:
    Project Euler 99:Largest exponential 最大的幂
    Project Euler 98:Anagramic squares 重排平方数
    Project Euler 97 :Large non-Mersenne prime 非梅森大素数
    Project Euler 96:Su Doku 数独
    Project Euler 95:Amicable chains 亲和数链
    Project Euler 94:Almost equilateral triangles 几乎等边的三角形
    Project Euler 93:Arithmetic expressions 算术表达式
    Project Euler 92:Square digit chains 平方数字链
    Project Euler 91:Right triangles with integer coordinates 格点直角三角形
    Project Euler 90:Cube digit pairs 立方体数字对
  • 原文地址:https://www.cnblogs.com/fish7/p/4164297.html
Copyright © 2011-2022 走看看