0
点赞
收藏
分享

微信扫一扫

java - 输入输出流I/O

you的日常 2022-01-24 阅读 95

文章目录

Java之

— 输入输出流I/O

参考 https://www.cnblogs.com/yichunguo/p/11775270.html

在这里插入图片描述

1. 字节流

  • FileInputStream / FileOutputStream
@Test
public void IOTest() {
    // 直接使用文件绝对路径创建流对象
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        // 直接使用文件绝对路径创建流对象
        fis = new FileInputStream("d:\\desktop\\1.txt");
        // 或者使用File对象创建流对象
        File FF = new File("d:\\desktop\\2.txt");
        fos = new FileOutputStream(FF, true);// 这个true表示追加续写,而不是清空再写数据

        int read;
        // fis.read() 读取到末尾,返回-1
        while ((read = fis.read()) != -1) {
            // 读取到的是ascll码,是字节,需要转换为字符才是我们认识的字母或数字
            System.out.println(read);
            fos.write(read);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // close 释放系统资源。该方法里面包含了flush方法。如果不关闭,数据只是保存到缓冲区,并未保存到文件。
        if (fos != null) {
            try {   // flush 主要是将缓冲区中,fos里面的数据写入并保存到文件。
                fos.flush();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (fis != null) {
            try {
                fis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

2. 字符流

  • FileReader / FileWriter
@Test
public void IOTest(){
    FileReader fr = null;
    FileWriter fw = null;
    try {
        fr = new FileReader("d:\\desktop\\1.txt");
        fw = new FileWriter("d:\\desktop\\2.txt");

        int read ;
        while((read = fr.read()) != -1){
            System.out.println( (char) read);
            fw.write(read);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (fw != null) {
            try {
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (fr != null) {
            try {
                fr.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

3. 字节缓冲流

  • BufferedInputStream / BufferedOutputStream
@Test
public void IOTest(){
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        bis = new BufferedInputStream(new FileInputStream("d:\\desktop\\1.txt"));
        bos = new BufferedOutputStream(new FileOutputStream("d:\\desktop\\2.txt"));

        int read ;
        while((read = bis.read()) != -1){
            System.out.println(read);
            bos.write(read);
        }
        // 更快
        /*int len;
            byte[] bytes = new byte[8*1024];
            while ((len = bis.read(bytes)) != -1) {
                System.out.println(read);
                bos.write(bytes, 0 , len);
            }*/
    } catch (Exception e) {
        e.printStackTrace();
    } finally{
        if(bos != null){
            try{
                bos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if(bis != null){
            try{ 
                bis.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

4. 字符缓冲流

  • BufferedReader / BufferedWriter
@Test
public void IOTest(){
    BufferedReader br = null;
    BufferedWriter bw = null;
    try {
        br = new BufferedReader(new FileReader("d:\\desktop\\1.txt"));
        bw = new BufferedWriter(new FileWriter("d:\\desktop\\2.txt"));

        int read ;
        while((read = br.read()) != -1){
            System.out.println( (char) read);
            bw.write(read);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally{
        if(bw != null){
            try{
                bw.close();
            } catch (Exception e){
                e.printStackTrace();
            }
        }
        if(br != null){
            try{
                br.close();
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

5. 字节流到字符流的转换

  • InputStreamReader
@Test
public void IOTest(){
    InputStreamReader isr1 = null;
    InputStreamReader isr2 = null;
    try {
        // 默认UTF8编码
        isr1 = new InputStreamReader(new FileInputStream("d:\\desktop\\1.txt"));
        // 创建流对象,并指定GBK编码
        isr2 = new InputStreamReader (new FileInputStream("d:\\desktop\\1.txt"), "GBK");

        int read ;

        while((read = isr1.read()) != -1){
            System.out.println( (char) read);// 乱码
        }
        isr1.close();

        while((read = isr2.read()) != -1){
            System.out.println( (char) read);
        }
        isr2.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

6. 字符流到字节流的转换

  • OutputStreamWriter
@Test
public void IOTest(){
    try {
        OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream("d:\\desktop\\2.txt"));

        fw.write("摸屎"); // 保存为6个字节 "UTF-8" 编码
        fw.close();
        OutputStreamWriter osw2 = new OutputStreamWriter(new FileOutputStream("d:\\desktop\\1.txt"),"GBK");

        osw2.write("摸屎");// 保存为4个字节
        osw2.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

7. 序列化流【理解】

  • 先创建一个 Employee 类,实现 Serializable 接口
public class Employee implements java.io.Serializable{
    public String name;
    public String address;
    public transient int age; // transient 瞬态修饰成员,不会被序列化
    public void addressCheck() {
        System.out.println("Address  check : " + name + " -- " + address);
    }
}
  • 再写一个测试类
@Test
public void IOTest(){
    Employee emp = new Employee();
    emp.name = "zhangsan";
    emp.address = "sichuan-chengdu";
    emp.age = 20;
    try {
        FileOutputStream fos = new FileOutputStream("d:\\desktop\\2.txt");
        ObjectOutputStream out = new ObjectOutputStream(fos);
        // 写出对象
        out.writeObject(emp);
        out.close();
        fos.close();

        System.out.println("Serialized data is saved"); // 姓名,地址被序列化,年龄没有被序列化。

        System.out.println("========反序列化中===========");
        Thread.sleep(5000);

        FileInputStream fis = new FileInputStream("d:\\desktop\\2.txt");
        ObjectInputStream oin = new ObjectInputStream(fis);
        // 读取一个对象
        emp = (Employee) oin.readObject();
        oin.close();
        fis.close();

        System.out.println("Name: " + emp.name);	// zhangsan
        System.out.println("Address: " + emp.address); // beiqinglu
        System.out.println("age: " + emp.age); // 0
        emp.addressCheck(); // 调用方法
    } catch (Exception e) {
        e.printStackTrace();
    }
}

8. Properties

  • public Object setProperty(String key, String value) : 保存一对属性。
  • public String getProperty(String key) :使用此属性列表中指定的键搜索属性值。
  • public Set<String> stringPropertyNames() :所有键的名称的集合。

8.1 与流相关的方法:

  • 2.txt – 键值对可以用 空格、等号、冒号等符号分割
name=zhangsan
localtion SCCD
age=18 
  • 测试类
@Test
public void IOTest(){
    try {
        Properties pro = new Properties();
        // 加载文本中信息到属性集
        pro.load(new FileInputStream("d:\\desktop\\2.txt"));
        // 遍历集合并打印
        Set<String> strings = pro.stringPropertyNames();
        for (String key : strings ) {
            System.out.println(key+" -- "+pro.getProperty(key));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
ocaltion SCCD
age=18 
  • 测试类
@Test
public void IOTest(){
    try {
        Properties pro = new Properties();
        // 加载文本中信息到属性集
        pro.load(new FileInputStream("d:\\desktop\\2.txt"));
        // 遍历集合并打印
        Set<String> strings = pro.stringPropertyNames();
        for (String key : strings ) {
            System.out.println(key+" -- "+pro.getProperty(key));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
举报

相关推荐

0 条评论