zoukankan      html  css  js  c++  java
  • 其他流

    ObjectInput/OutputStream

    分别与FileOutputStream和FileInputStream一起使用时,可以为应用程序提供对对象的持久存储.

    我们把对象以某种特定的编码格式写入称之为"序列化"

    把写入的编码格式内容还原成对象称之为"反序列化"

    被序列化的对象必须实现Serializable接口

        

    package otherio;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    public class ObjectInputOutputDemo {
         public static void main(String[] args) throws IOException {
        /*    Student student=new Student("张三", 20);
            FileOutputStream fos=new FileOutputStream(new File("D:\a.txt"));
            ObjectOutputStream os=new ObjectOutputStream(fos);
            ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream(new File("D:\a.txt")));
            os.writeObject(student);//把对象序列化到指定的文件输出流中
            os.close();*/
             
            ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File("D:\\a.txt")));
            try {
                Student student=(Student) ois.readObject();
                System.out.println(student.getName()+" "+student.getAge());
            } catch (ClassNotFoundException e) {
                
                e.printStackTrace();
            }
        
            
        }
    }
    
    class Student implements Serializable{
    
        
        /**
         * 
         */
        private static final long serialVersionUID = 3655643694434659371L;
        private String name;
        private int age;
        private String address;
        
        
        public Student(String name, int age) {
        
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        
    }

    InputStreamReader/OutputStreamWriter

      转换流  指将字节流与字符流之间的转换

    package otherio;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    
    
    public class InputStreamReaderWriterDemo {
      public static void main(String[] args) throws IOException {
          /*OutputStreamWriter w=new OutputStreamWriter(new FileOutputStream(new File("D:\aa.txt")), "utf-8");
          BufferedWriter bw=new BufferedWriter(w);
          bw.write("你好");
          bw.close();*/
          
          BufferedReader bReader=new BufferedReader(new InputStreamReader(
                  new FileInputStream(new File("D:\aa.txt")), "utf-8"));
          String line =null;
          while((line=bReader.readLine())!=null) {
            System.out.println(line);          
           }
          bReader.close();
      }
    }

    RandomAccessFile

      随机访问文件

    package otherio;
    
    import java.io.IOException;
    import java.io.RandomAccessFile;
    import java.util.Scanner;
    
    public class RandomAccessFileDemo {
    public static void main(String[] args) throws IOException {
        Person[] persons= {new Person("zhangs",90),new Person("justin",30),new Person("bush",88),new Person("lisi",20)};
        RandomAccessFile reFile= new RandomAccessFile("D:\a.txt", "rw");
        //写入数据到RandomAccessFile中
        for (int i = 0; i < persons.length; i++) {
            reFile.writeChars(persons[i].getName());
            reFile.writeChar(persons[i].getAge());
        }
        
        //读取指定位置的person对象
        Scanner scanner=new Scanner(System.in);
        System.out.println("读取第几个oerson对象数据");
        int  num=scanner.nextInt();
        //使用seek方法来操作存取位置
        reFile.seek((num-1)*Person.size());
        Person person=new Person();
        person.setName(readName(reFile));
        person.setAge(reFile.readInt());
        System.out.println(person.getName()+person.getAge());
        reFile.close();
    }
            private static String  readName(RandomAccessFile randomAccessFile) throws IOException {
                char[] name=new char[15];
                for (int i = 0; i < name.length; i++) {
                    name[i]=randomAccessFile.readChar();
                }
                return new String(name).replace('u0000', ' ');
            }
    }
    
    class Person {
        private String name;
        private int age;
    
        public Person() {
            super();
        }
    
        public Person(String name, int age) {
            StringBuilder builder = null;
            if (name != null) {
                builder = new StringBuilder(name);
            } else {
                builder = new StringBuilder(15);
            }
            builder.setLength(15);// 固定长度为15个,占了30个字节的大小,后面以空字符'u0000'
            this.name = builder.toString();
            this.age = age;
    
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            StringBuilder builder = null;
            if (name != null) {
                builder = new StringBuilder(name);
            } else {
                builder = new StringBuilder(15);
            }
            builder.setLength(15);// 固定长度为15个,占了30个字节的大小,后面以空字符'u0000'
            this.name = builder.toString();
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    
        // 每个对象所占的字节数
        public static int size() {
            return 34;
        }
    
    }
  • 相关阅读:
    linux面试常问命令
    图卷积网络 GCN Graph Convolutional Network(谱域GCN)的理解和详细推导
    总结一些牛客网上 的算法岗面试题
    Pytorch-Tensor基本操作
    C++使用代码创建一个Windows桌面应用程序
    Windows中的消息与消息队列
    解决COM组件在WPF设计器中命名空间不存在XXX的问题(附带如何在WPF中使用APlayer引擎)
    Mybatis的一级缓存
    面试题:zookeeper实现分布式锁
    sublime text 文件默认打开格式
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/10035601.html
Copyright © 2011-2022 走看看