当前位置:   article > 正文

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

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

字符编码

1.编码解码

2.字符集(码表) 

3.常见编码规则 

4.Java中的编解码方法

IO字符流

1.字符流读的基本使用

  1. package com.lu.day11.test;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. public class Test {
  5. public static void main(String[] args) {
  6. try(FileReader fileReader = new FileReader("resource/b.txt");) {
  7. char[] chars = new char[1024];
  8. int read;
  9. while ((read = fileReader.read(chars)) != -1) {
  10. System.out.println(new String(chars, 0, read));
  11. }
  12. } catch (IOException e) {
  13. throw new RuntimeException(e);
  14. }
  15. }
  16. }

2.字符流写的基本使用

  1. package com.lu.day11.test;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. public class Test1 {
  5. public static void main(String[] args) {
  6. try (FileWriter fileWriter = new FileWriter("resource/a.txt");){
  7. fileWriter.write("hello");
  8. } catch (IOException e) {
  9. throw new RuntimeException(e);
  10. }
  11. }
  12. }

3.BufferedReader

  1. package com.lu.day11.test;
  2. import java.io.BufferedReader;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. public class Test {
  6. public static void main(String[] args) {
  7. try(BufferedReader bufferedReader = new BufferedReader(new FileReader("resource/b.txt"))) {
  8. //判断是否读取结束
  9. while (bufferedReader.ready()){
  10. //读取一行
  11. System.out.println(bufferedReader.readLine());
  12. //jdk8新特性
  13. //bufferedReader.lines().forEach(System.out::println);
  14. }
  15. } catch (IOException e) {
  16. throw new RuntimeException(e);
  17. }
  18. }
  19. }

4.BufferedWriter

  1. package com.lu.day11.test;
  2. import java.io.BufferedWriter;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. public class Test1 {
  6. public static void main(String[] args) {
  7. try ( BufferedWriter bufferedWriter = new BufferedWriter( new FileWriter("resource/a.txt",true))){
  8. bufferedWriter.write("hello world");
  9. //换行屏蔽不同操作系统差异性
  10. bufferedWriter.newLine();
  11. } catch (IOException e) {
  12. throw new RuntimeException(e);
  13. }
  14. }
  15. }

序列化反序列化

 1.序列化的基本使用

  1. package com.lu.day11.test;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectOutputStream;
  5. public class Test2 {
  6. public static void main(String[] args) {
  7. try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));){
  8. out.writeObject(new Student("张三", 18));
  9. } catch (IOException e) {
  10. throw new RuntimeException(e);
  11. }
  12. }
  13. }

  1. package com.lu.day11.test;
  2. import java.io.Serializable;
  3. //使用序列化要使用Serializable接口
  4. public class Student implements Serializable {
  5. private String name;
  6. private int age;
  7. public Student(String name, int age) {
  8. this.name = name;
  9. this.age = age;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public int getAge() {
  18. return age;
  19. }
  20. public void setAge(int age) {
  21. this.age = age;
  22. }
  23. }

2.反序列化的基本使用

  1. package com.lu.day11.test;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectInputStream;
  5. import java.util.ArrayList;
  6. public class Test3 {
  7. public static void main(String[] args) {
  8. try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("student.txt"))){
  9. ArrayList<Student> o = (ArrayList<Student>) in.readObject();
  10. System.out.println(o);
  11. } catch (IOException | ClassNotFoundException e) {
  12. throw new RuntimeException(e);
  13. }
  14. }
  15. }
  1. package com.lu.day11.test;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.ObjectOutputStream;
  5. import java.util.ArrayList;
  6. public class Test2 {
  7. public static void main(String[] args) {
  8. try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("student.txt"));){
  9. ArrayList<Student> list = new ArrayList<>();
  10. list.add(new Student("李四", 19));
  11. out.writeObject(list);
  12. } catch (IOException e) {
  13. throw new RuntimeException(e);
  14. }
  15. }
  16. }

Properties

1.properties写

  1. package com.lu.day11.test;
  2. import java.io.FileWriter;
  3. import java.util.Properties;
  4. public class Test4 {
  5. public static void main(String[] args) {
  6. Properties properties = new Properties();
  7. properties.setProperty("name", "lu");
  8. properties.setProperty("age", "18");
  9. try (FileWriter fileWriter = new FileWriter("resource/test.properties");){
  10. //comments 注释在文件里中文会保存为unicode编码
  11. //.properties
  12. properties.store(fileWriter, "这是练习");
  13. } catch (Exception e) {
  14. throw new RuntimeException(e);
  15. }
  16. }
  17. }

2.properties读 

  1. package com.lu.day11.test;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.Properties;
  6. public class Test5 {
  7. public static void main(String[] args) {
  8. Properties properties = new Properties();
  9. try( FileReader fileReader = new FileReader("resource/test.properties");){
  10. properties.load(fileReader);
  11. properties.forEach((k,v)->System.out.println(k+"--->"+v));
  12. } catch (IOException e) {
  13. throw new RuntimeException(e);
  14. }
  15. }
  16. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/869669
推荐阅读
相关标签
  

闽ICP备14008679号