JAVASE进阶day11(IO流(字符流),编码格式,序列化)

字符编码

1.编码解码

2.字符集(码表) 

3.常见编码规则 

4.Java中的编解码方法

IO字符流

1.字符流读的基本使用

package com.lu.day11.test;

import java.io.FileReader;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try(FileReader fileReader = new FileReader("resource/b.txt");) {

            char[] chars = new char[1024];
            int read;
            while ((read = fileReader.read(chars)) != -1) {
                System.out.println(new String(chars, 0, read));

            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

2.字符流写的基本使用

package com.lu.day11.test;

import java.io.FileWriter;
import java.io.IOException;

public class Test1 {
    public static void main(String[] args) {
        try (FileWriter fileWriter = new FileWriter("resource/a.txt");){

            fileWriter.write("hello");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

3.BufferedReader

package com.lu.day11.test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader("resource/b.txt"))) {
            //判断是否读取结束
            while (bufferedReader.ready()){
                //读取一行
                System.out.println(bufferedReader.readLine());

                //jdk8新特性
                //bufferedReader.lines().forEach(System.out::println);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

4.BufferedWriter

package com.lu.day11.test;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class Test1 {
    public static void main(String[] args) {
        try ( BufferedWriter bufferedWriter = new BufferedWriter( new FileWriter("resource/a.txt",true))){

            bufferedWriter.write("hello world");
            //换行屏蔽不同操作系统差异性
            bufferedWriter.newLine();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

序列化反序列化

 1.序列化的基本使用

package com.lu.day11.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class Test2 {
    public static void main(String[] args) {
        try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));){
            out.writeObject(new Student("张三", 18));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

package com.lu.day11.test;

import java.io.Serializable;
//使用序列化要使用Serializable接口
public class Student implements Serializable {
    private String name;
    private int age;

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

    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;
    }
}

2.反序列化的基本使用

package com.lu.day11.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class Test3 {
    public static void main(String[] args) {
        try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("student.txt"))){
            ArrayList<Student> o = (ArrayList<Student>) in.readObject();
            System.out.println(o);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}
package com.lu.day11.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class Test2 {
    public static void main(String[] args) {
        try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));){
            ArrayList<Student> list = new ArrayList<>();
            list.add(new Student("李四", 19));
            out.writeObject(list);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Properties

1.properties写

package com.lu.day11.test;

import java.io.FileWriter;
import java.util.Properties;

public class Test4 {
    public static void main(String[] args) {
        Properties properties = new Properties();
        properties.setProperty("name", "lu");
        properties.setProperty("age", "18");
        try (FileWriter fileWriter = new FileWriter("resource/test.properties");){
            //comments 注释在文件里中文会保存为unicode编码
            //.properties
            properties.store(fileWriter, "这是练习");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

2.properties读 

package com.lu.day11.test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Test5 {
    public static void main(String[] args) {
        Properties properties = new Properties();
        try( FileReader fileReader = new FileReader("resource/test.properties");){
            properties.load(fileReader);
            properties.forEach((k,v)->System.out.println(k+"--->"+v));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值