美文网首页技术栈
2019-04-29——Java IO 字节流

2019-04-29——Java IO 字节流

作者: 烟雨乱平生 | 来源:发表于2019-04-30 00:11 被阅读0次
    public class Student implements Serializable {
        private String name;
        private int age;
        private String agent;
        transient private Date createdDate;
    
        public Student(String name, int age, String agent, Date createdDate) {
            this.name = name;
            this.age = age;
            this.agent = agent;
            this.createdDate = createdDate;
        }
    
        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;
        }
    
        public String getAgent() {
            return agent;
        }
    
        public void setAgent(String agent) {
            this.agent = agent;
        }
    
        public Date getCreatedDate() {
            return createdDate;
        }
    
        public void setCreatedDate(Date createdDate) {
            this.createdDate = createdDate;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", agent='" + agent + '\'' +
                    '}';
        }
    }
    
    private void useByteArrayInputStream(String value) throws IOException {
            byte[] data = new byte[1024];
            ByteArrayInputStream bais = new ByteArrayInputStream(value.getBytes());
            int size;
            while((size = bais.read(data)) !=-1){
                String content = new String(data,0,size);
                System.out.println(content);
            }
            bais.close();
        }
    
        private void useByteArrayOutputStream(String value) throws IOException {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.write(value.getBytes());
            byte[] data = baos.toByteArray();
            baos.close();
            String content = new String(data,0,data.length);
            System.out.println(content);
        }
    
        private void useFileInputStream(String filePath) throws IOException {
            byte[] data = new byte[1024];
            FileInputStream fis = new FileInputStream(filePath);
            int size;
            while((size = fis.read(data)) !=-1){
                String content = new String(data,0,size);
                System.out.println(content);
            }
            fis.close();
        }
    
        private void useFileOutputStream(String filePath,String content) throws IOException {
            FileOutputStream fos = new FileOutputStream(filePath);
            fos.write(content.getBytes());
            fos.flush();
            fos.close();
        }
    
        private void useDataInputStream(String filePath) throws IOException {
            byte[] data = new byte[1024];
            DataInputStream dis = new DataInputStream(new FileInputStream(filePath));
            int size;
            while ((size = dis.read(data))!=-1){
                String content = new String(data,0,size);
                System.out.println(content);
            }
            dis.close();
        }
    
        private void useDataOutputStream(String filePath) throws IOException {
            DataOutputStream dos = new DataOutputStream(new FileOutputStream(filePath));
            dos.writeUTF("my name is wang,and my age is ");
            dos.writeInt(20);
            dos.flush();
            dos.close();
        }
    
        private void useBufferedInputStream(String filePath) throws IOException {
            byte[] data = new byte[1024];
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
            int size;
            while ((size = bis.read(data))!=-1){
                String content = new String(data,0,size);
                System.out.println(content);
            }
            bis.close();
        }
    
        private void useBufferedOutputStream(String filePath,String content) throws IOException {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
            bos.write(content.getBytes());
            bos.flush();
            bos.close();
        }
    
    
    
        private void useObjectInputStream(String filePath) throws IOException, ClassNotFoundException {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));
            List<Student> students = (List<Student>) ois.readObject();
            ois.close();
            System.out.println(students);
        }
    
    
        private void useObjectOutputStream(String filePath) throws IOException {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
            oos.writeObject(getData());
            oos.flush();
            oos.close();
        }
    

    相关文章

      网友评论

        本文标题:2019-04-29——Java IO 字节流

        本文链接:https://www.haomeiwen.com/subject/zzppnqtx.html