[JavaEE]字符流 缓冲流 转换流 打印流 序列化流 反序列化流

  1. FileWriter类(神似FileOutputSream)

    1. 也有一个缓冲区,8192个字节,使用flush可以使缓冲区的数据传到文件中,或者关流时也能传到文件中,或者缓冲区满了,自动装到文件中,
    2. write()方法能把数据放到缓冲区当中
  2. 字符流底层原理:

  3. 文件拷贝:

    复制代码
    public static void copyDir(File file1,File file2) throws IOException {
            
    file2.mkdirs();
    File[] files = file1.listFiles();
    if(files==null) return ;
    for (File file : files) {
        if(file.isFile()){
            FileInputStream fr=new FileInputStream(file);
            FileOutputStream fw=new FileOutputStream(new File(file2,file.getName()));
            byte[] bytes=new byte[1024*8];
            int read;
            while((read=fr.read(bytes))!=-1)
                fw.write(bytes,0,read);
            fr.close();
            fw.close();
        }else{
            String name = file.getName();
            File file3=new File(file2,name);
            copyDir(file,file3);
        }
    }
  4. 通过一个数字异或同一个数字两次就能还原的特性实现文件加密

  5. 实现文件修改: 1.

27.初识缓冲流:

  1. 字节缓冲输入流(Java在底层维护了一个长度为8192的字节数组缓冲区)

  2. 字节缓冲输出流

    //他会很快嘛,为什么呢,底层是什么实现呢
    public static void bufferHighCopy(File file1,File file2) throws IOException {
    BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file1));
    BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(file2));
    int len=0;
    while((len=bis.read())!=-1){
    bos.write(len);
    }
    bos.close();
    bis.close();
    }

  3. 字符缓冲流

    1. 字符缓冲输入流BufferedReader
    2. 字符缓冲输出流BufferedWriter

    底层有一个长度为8192的字符数组(char类型的),Java中一个char表示2个字符

    有两个方法

    1. newLine(),输出时跨平台实现换行功能
    2. readLine():输入时一读读一行,不读取换行符,没什么能读的就返回null
  4. 转换流

    1. 在字节流和字符流之间进行转换

    2. InputStreamReader和OutputStreamWriter

      //使用指定的编码规则打开文件
      FileReader fr=new FileReader("C:\\ai-output\\test\\xilu.txt",Charset.forName("GBK"));
      int b;
      while((b=fr.read())!=-1){
      System.out.print((char)b);
      }
      fr.close();

  5. 题目:使用字节流打开纯文本文件,要求不能出现乱码,一下子能读取一行

    复制代码
    BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("C:\\\\ai-output\\\\test\\\\xilu.txt"),Charset.forName("GBK")));
    String str;
    while((str=br.readLine())!=null){
        System.out.println(str);
    }
    br.close();
  6. 序列化流/对象操作输出流

    1. 把Java对象写到文件中
    2. 对象要实现Serializable接口
    3. 要有一个private static final long serialVersionUID = 7310462743447418622L;然后就能保证序列化时写入对象,然后修改对象,然后反序列化后不报错
    4. transient int age; 使用transient瞬态关键字可以把这个字段不序列化到文件中

    反序列化流/对象操作输入流,将文件变成java对象

    复制代码
    Student s=new Student("Rance",23);
    ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("javabean.txt"));
    oos.writeObject(s);
    oos.close();
    
    ObjectInputStream ois=new ObjectInputStream(new FileInputStream("javabean.txt"));
    Object o = ois.readObject();
    System.out.println(o);
    ois.close();
  7. 在使用序列化流和反序列化流的时候,如果需要序列化多个对象,需要把所有的对象放到一个集合中,然后序列化集合,以此来解决反序列化时不知道该调用几次readObject()方法的问题

    Student s1=new Student("Rance",10);
    Student s2=new Student("Xilu",20);
    Student s3=new Student("Zhijinxiang",30);
    ArrayList<Student> list=new ArrayList<>();
    list.add(s1);
    list.add(s2);
    list.add(s3);
    ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("rance.txt"));
    oos.writeObject(list);
    oos.close();
    ObjectInputStream ois=new ObjectInputStream(new FileInputStream("rance.txt"));
    ArrayList<Student> o = (ArrayList<Student>) ois.readObject();
    System.out.println(o);
    ois.close();

  8. 字节输出流PrintStream

    PrintStream ps=new PrintStream(new FileOutputStream("rance.txt"),true,"GBK");
    ps.println(98982543d);
    ps.println("java");
    ps.print(true);
    ps.printf("%s 重击","兰斯");
    ps.close();

  9. System.out.println()是标准输出流,是PrintStream类型的,系统唯一,不可关闭

相关推荐
用户5671504710214 分钟前
OpenClaw 记忆管理系统技术文档
算法
smchaopiao24 分钟前
Python中字典与列表合并的问题与解决方法
开发语言·python
wsx_iot24 分钟前
TDengine学习
数据库·学习·tdengine
9359626 分钟前
练习题53-60
算法·深度优先
卡尔特斯34 分钟前
Ultralytics YOLO26 自动对指定标注文件夹区分标注素材脚本与训练脚本
python·openai
霖大侠40 分钟前
Wavelet Meets Adam: Compressing Gradients forMemory-Efficient Training
人工智能·深度学习·算法·机器学习·transformer
敲代码的瓦龙41 分钟前
Java?面向对象三大特性!!!
java·开发语言
2501_9216494942 分钟前
期货 Tick 级数据与基金净值历史数据 API 接口详解
开发语言·后端·python·websocket·金融·区块链
架构师沉默44 分钟前
AI 写的代码,你敢上线吗?
java·后端·架构