[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 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 o = (ArrayList) 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类型的,系统唯一,不可关闭

相关推荐
2401_841495645 小时前
【操作系统】进程同步与互斥实验报告
c++·算法·操作系统·进程·并发·同步·互斥
码智社6 小时前
AES加密原理详解及Java实现加解密实战
java·开发语言
AI云海6 小时前
python 列表、元组、集合和字典
开发语言·python
克里斯蒂亚诺更新6 小时前
ruoyi角色权限的对应关系
笔记
二十雨辰6 小时前
[爬虫]-Urllib
爬虫·python
萧瑟余晖7 小时前
JDK 26 新特性详解
java·开发语言
马优晨7 小时前
Freemarker 完整讲解(后端 Java 模板引擎)
java·开发语言·freemarker·freemarker 完整讲解·freemarker模板引擎
玉鸯8 小时前
Agent Hook:在概率推理之上,为 Agent 叠加确定性控制
python·langchain·agent
weixin_446260859 小时前
HACO:面向动态部署环境的对冲式智能计算可靠多智能体调度框架
后端·python·flask