I/O 再 review

I/O 再 review

 

 

package com.test.leon;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.net.URL;

import org.omg.PortableInterceptor.DISCARDING;

public class DirectActFileStream {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        // 1. Source
        File src = new File("c:/Leon/abc.txt");
        File desc = new File("c:/Leon/to.txt");
        
        // 2. choose stream
        /*-- FileInputStream, FileOutputStream --*/
        FileInputStream fis = new FileInputStream(src); 
        FileOutputStream fos = new FileOutputStream(desc);
        // 3. prepare & read / write
        byte[] temp1 = new byte[1024]; 
        int len1 = -1; 
        len1 = fis.read(temp1);    // read
        fos.write(temp1);        // write
        // 4. close file    
        fis.close(); 
        fos.close();
        
        // 2. choose stream
        /*-- FileReader, FileWriter --*/
        FileReader fr = new FileReader(src);
        FileWriter fw = new FileWriter(desc);
        // 3. preapare, read, write
        char temp2[] = new char[1024];
        int len2 = -1;
        len2 = fr.read(temp2);    // read
        fw.write("asdfasdfasdf"); // write
        // 4. close file
        fr.close();
        fw.close();
        
    /* -- add buffer --*/
        // 2. choose stream
        /*-- BufferInputStream, BufferOutputStream --*/
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desc));
        // 3. prepare & read / write
        byte[] temp3 = new byte[1024]; 
        int len3 = -1; 
        len3 = bis.read(temp3);    // read
        bos.write(temp3);        // write
        // 4. close file    
        bis.close(); 
        bos.close();

        // 2. choose stream
        /*-- BufferedReader, BufferedWriter --*/
        BufferedReader br = new BufferedReader(new FileReader(src));
        BufferedWriter bw = new BufferedWriter(new FileWriter(desc));
        // 3. preapare, read, write
        char temp4[] = new char[1024];
        int len4 = -1;
        len4 = br.read(temp4);    // read
        bw.write("asdfasdfasdf"); // write
        // 4. close file
        br.close();
        bw.close();
    
    /* -- non file, maybe from memory -- */
        // String(memory) -> ByteArrayInputStream(memory) -> ByteArrayOutputStream(memory) -> byte[] array(memory) -> 输出屏幕.
        // 1. source
        String sourceMemory = "xyzabcdefg";
        byte[] src5 = sourceMemory.getBytes();    // from memory
        // 2. choose stream
        ByteArrayInputStream bais = new ByteArrayInputStream(src5);
        // baos 本身就是一个存储, 存储在内存中, 所以, baos.write 是写入到内存中的 baos 区域了.
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // 3. prepare, read, write, 需要逐个字节读取
        int len5 = -1;
        byte[] destMemory;
        while (null != bais &&
            (len5 = bais.read()) != -1) {
            System.out.println((char)len5);
            baos.write(len5);    // baos 对应的内存中就保存了这一串字符串
        }
        baos.flush();    // 相关的 内存中的 buffer 被清空
        destMemory = baos.toByteArray();    // 将 baos 内存区域中存储的内容输出到字节数组
        for (int i = 0; i < destMemory.length; i++) {
            System.out.println((char)destMemory[i]);
        }
        // 4. 无需close file, 因为本次没有文件操作.
        
    /* convert 字节 -> 字符 */
        //1. source is web 字节流
        //2. choose stream
        BufferedReader cbr = new BufferedReader(
            new InputStreamReader(
                new URL("http://www.baidu.com").openStream(), "UTF-8"));
        BufferedWriter cbw = new BufferedWriter(new FileWriter(desc));
        // 3. preapare, read, write
        char temp6[] = new char[1024];
        int len6 = -1;
        len6 = cbr.read(temp6);    // read
        cbw.write(temp6); // write
        // 4. close file
        cbr.close();
        cbw.close(); 
        
    /* convert 字符 -> 字节 */
        //1. source, 字符流
        String msg = "abcdefghigk";
        //2. choose stream
        BufferedWriter xbw = new BufferedWriter(
            new OutputStreamWriter(
                new FileOutputStream(desc), "UTF8"));
        //3. prepare, read, write
        xbw.write(msg);
        //4. close
        xbw.close();
        
    /* simulate network transfer DataInputStream, DataOutputStream*/
        //1. source, 按照data的格式
        //2. choose stream
        //3. 模拟发送
        DataOutputStream dos = new DataOutputStream(baos);    //baos simulate memory;
        dos.writeInt(18);
        dos.writeBoolean(false);
        dos.flush();
        //4.模拟接收
        byte[] datas = baos.toByteArray();
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(datas));
        int age = dis.readInt();
        boolean flag = dis.readBoolean();
        System.out.println("--" + age + "--" + flag);
    /* 序列化:对象->字节流, 反序列化: 字节流->对象 */
        //1.source
        Employee emp = new Employee("X man", 100.00);
        //2.choose steam
        // 将对象写入该文件
        ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(desc)));
        oos.writeObject(emp);
        oos.flush();
        //3. 从该文件读取对象
        ObjectInputStream ois = new ObjectInputStream(
                                    new BufferedInputStream(
                                        new FileInputStream(desc)));
        Object newObj = ois.readObject();
        if (newObj instanceof Employee) {
            Employee empTwins = (Employee)newObj;
            System.out.println(empTwins);
        }
    }
    
}

// 只有实现 Serializable 接口才能被序列化
class Employee implements Serializable {
    private String name;
    private double score;
    public Employee() {}
    public Employee(String name, double score) {
        super();
        this.name = name;
        this.score = score;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    @Override
    public String toString() {
        return "Employee [name=" + name + ", score=" + score + "]";
    }
    
    
    
}

 

上一篇:团队Github实践训练


下一篇:【JUC】JDK1.8源码分析之SynchronousQueue(九)