java io流(小白版)

一.java io流分类

1.节点流

		理解为导线中的铜丝。

2.过滤流

		理解为导线的外包皮层(包装作用)。

二.io操作的基本四步

1.创建节点流
2.包装过滤流
3.读写数据
4.关闭流

三.节点流

1.FileInputStream(字节输入流) 和FileOutputStream(字节输出流)

public class TestFileCopy {
    public static void main(String[] args) throws Exception {
        long start = System.nanoTime();
        FileInputStream in = new FileInputStream("E:\\copyFile\\aa.wmv");
        FileOutputStream out = new FileOutputStream("E:\\copyFile\\new.wmv");
        int length = 0;
        byte[] bytes = new byte[1024];
        while((length = in.read(bytes)) != -1){
            out.write(bytes, 0, length);
        }
        long end = System.nanoTime();
        in.close();
        out.close();
        System.out.println((end-start)/1E9);
    }
}

四.过滤流

1.BufferedInputStream 和BufferedOutputStream

public class TestBufferStream {
    public static void main(String[] args) throws Exception {
        FileOutputStream out = new FileOutputStream("a.txt");
        BufferedOutputStream bout = new BufferedOutputStream(out);
        for (int i = 0; i < 100000; i++) {
            bout.write('a');
        }
//        bout.flush();//
        FileInputStream in = new FileInputStream("a.txt");
        BufferedInputStream bin = new BufferedInputStream(in);
        int cout = 0;
        while (bin.read() != -1) {
            cout++;
        }
        System.out.println(cout);
//        bout.flush();//清空缓冲区
//        bout.close();//自带flush

        BufferCopy();
    }

    public static void BufferCopy() throws Exception {
        long start = System.nanoTime();
        FileInputStream in = new FileInputStream("E:\\copyFile\\aa.wmv");
        FileOutputStream out = new FileOutputStream("E:\\copyFile\\new.wmv");
        BufferedOutputStream bout = new BufferedOutputStream(out);
        BufferedInputStream bin = new BufferedInputStream(in);
        int length = 0;
        byte[] bytes = new byte[1024];
        while((length = bin.read(bytes)) != -1){
            bout.write(bytes, 0, length);
        }
        long end = System.nanoTime();
        bin.close();
        bout.close();
        System.out.println((end-start)/1E9);
    }
}

bout.flush() 可以理解为男同胞撒尿最后抖的那下,将最后的尿尿排出

/** Flush the internal buffer */
    private void flushBuffer() throws IOException {
        if (count > 0) {
            out.write(buf, 0, count);//out就是new FileOutputStream("E:\\copyFile\\new.wmv")
            count = 0;
        }
    }

2.DataStream

public class DataFlieCopy {
    public static void main(String[] args) throws Exception {
        FileOutputStream out = new FileOutputStream("2.txt");
        long a = 1234567890L;
        DataOutputStream Dout = new DataOutputStream(out);
        Dout.writeLong(a);
        Dout.close();

        FileInputStream in = new FileInputStream("2.txt");
        DataInputStream Din = new DataInputStream(in);
        System.out.println(Din.readLong());
    }
}

3.PrintStream

public class TestPrintStream {
    public static void main(String[] args) throws Exception{
        FileOutputStream fileOutputStream = new FileOutputStream("sa.txt");
        PrintStream printStream = new PrintStream(fileOutputStream);
        System.setOut(printStream);
        System.out.println(124349L);
    }
}

4.ObjectOutputStream 和ObjectInputStream

public class TestObjectStream {
    public static void main(String[] args) throws Exception{
        Student zhang = new Student("zhang", 30, 30.1);
        Student hu = new Student("hu", 14, 100.1);

        FileOutputStream fops = new FileOutputStream("students.dat");
        ObjectOutputStream out = new ObjectOutputStream(fops);
        out.writeObject(zhang);
        out.writeObject(hu);
        out.close();

        FileInputStream fileInputStream = new FileInputStream("students.dat");
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            try {
                while (true) {
                    Object object = objectInputStream.readObject();
                    System.out.println(object);
                }
            } catch (EOFException e) {
                System.out.println("文件读完了");
            }
        objectInputStream.close();
    }
}

class Student implements Serializable {
    private static final long serialVersionUID = -123214234L;
    String id;
    String name;
    transient int age;//不会被序列化
    double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
}

这个对象流配合序列化有很多知识点,有个Object.clone的知识点(深浅拷贝问题)。

二.java io流其他情况

inputStream的其它子类

public static InputStream downloadFile(String groupName, String remoteFileName) throws IOException, MyException {
        //创建一个Tracker访问的客户端对象TrackerClient
        TrackerClient trackerClient = new TrackerClient();
        //通过TrackerClient访问TrackerServer服务,获取连接信息
        TrackerServer trackerServer = trackerClient.getConnection();
        //通过TrackerServer的连接信息可以获得Storage的连接信息,创建Storageclient对象存储Storage的连接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        //回去对应文件的字节数组
        byte[] bytes = storageClient.download_file(groupName, remoteFileName);

        return new ByteArrayInputStream(bytes);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值