IO流

 流操作的目的:完成内存数据和磁盘数据的转换。

流按照方向分:输入流和输出流,以内存作为参照物。
当从数据源中将数据读取到内存中时,叫输入流(读取流)。
当将内存中的数据写入要数据源时,叫输出流(写入流)。

流按照传输的内容分:字节流和字符流,对象流。
无论是哪一种流,底层部分都是以字节方式传输,所以,其本质都是字节流。但是为了方便程序员更好的操作字符数据和对象数据,所以在字节流基础上做了一层包装,形成了字符流和对象流。


流操作的步骤:
1. 建立流;
2. 操作流;
3. 关闭流;

流操作以后必须关闭操作,否则不仅浪费内存资源,而且写入流有可能写不进数据。

操作文件流时,如果文件不存在,那么读取流会抛出文件未找到异常,而写入流,会创建新文件。


[字节流的父类:InputStream(读取流)  ][ OutputStream(写入流)]
// 读取
public void inputFile() {
InputStream input = null;
        
try {
    //建立文件读取字节流
    input = new FileInputStream("D:/a.jpg");
            
    byte[] by = new byte[1024];
    int len = 0;
    //将流中的数据读取到字节数组中,返回当前读取的字节数,读取完毕,返回-1
    //read()从此输入流中将最多 by.length 个字节的数据读入一个 byte 数组中
    while ((len = input.read(by)) != -1) {
        System.out.println(len);
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
//写入
public void outputFile(String str) {
        
    OutputStream out = null;
    try {
        //第二个参数如果是true表示追加方式写入数据
        out = new FileOutputStream("D:/b.txt",true);
            
        out.write(str.getBytes());
            
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
//拷贝
public void copyFile() {
        
    InputStream input = null;
    OutputStream out = null;
    try {
        input = new FileInputStream("D:/a.txt");
        out = new FileOutputStream("D:/b.txt");
            
        byte[] by = new byte[1024];
        int len = 0;
        while ((len = input.read(by)) != -1) {
            //读取了几个字节,就写入几个字节
            out.write(by, 0, len);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            out.close();
            input.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
    

[字符流的父类:Reader        ][        Writer]
//读取
public void reader() {
    Reader read = null;
    BufferedReader bu = null;
        
    try {
        //建立文件读取流
        read = new FileReader("D:/a.txt");
        //套接流,在一个流的基础上,再套接一个流,也称为高级流
        bu = new BufferedReader(read);
        String str = null;
        while ((str = bu.readLine()) != null) {
            System.out.println(str);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            bu.close();
            read.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


//写入
public void writer() {
    Writer wri = null;
        
    try {
        //第二个参数如果是true表示追加方式写入数据
        wri = new FileWriter("D:/1.txt",true);
        wri.write("bilibili");
            
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            wri.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


对象流

序列化:当需要堆对象进行传输时由于对象中的数据很庞大,无法直接传输,那么在传输之前需要将对象打散成二进制的序列,一遍遍传输,这个过程称为序列化过程,到达目的地后,有需要将二进制序列还原成对象,这个过程称为反序列化过程。


所有需要实现对象序列化的对象必须实现Serializable接口

//transient是属性修饰符,加上transient后,表示该属性的值不进行传输
private transient int score;

猜你喜欢

转载自blog.csdn.net/qq_40709929/article/details/80040868