java基础-IO流-day13

1. IO的概念

计算机内存中的数据 <--> 硬盘里面的数据
也就是数据的落盘 以及 数据的读取

文件的操作

package com.msb.io01;

import java.io.File;
import java.io.IOException;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 20:32
 * @Description: com.msb.io01
 * @version: 1.0
 */
public class Test01 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:"+ File.pathSeparator+"test.txt");
        System.out.println(f.canRead());
        System.out.println(f.canWrite());
        System.out.println(f.getName());
        System.out.println(f.isDirectory());
        System.out.println(f.isFile());
        System.out.println(f.isHidden());
        System.out.println(f.length());
        System.out.println(f.exists());

/*        if(f.exists()){
            f.delete();
        }else{
            f.createNewFile();
        }*/
        //路径相关
        System.out.println(f.getAbsolutePath());
        System.out.println(f.getPath());
        System.out.println(f.toString());

        System.out.println("--------------");
        File f5 = new File("demo.txt");
        if(!f5.exists()){
            f5.createNewFile();
        }
        File f6 = new File("a/b/c/demo.txt");
        if(!f6.exists()){
            f6.createNewFile();
        }
        System.out.println(f6.getAbsolutePath());//绝对路径
        System.out.println(f6.getPath());//相对路径
    }
}

目录的操作

package com.msb.io01;

import java.io.File;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 20:42
 * @Description: com.msb.io01
 * @version: 1.0
 */
public class Test02 {
    public static void main(String[] args) {
        File f = new File("C:\\Users\\chenw\\IdeaProjects\\Testuntitled");
        System.out.println("文件是否可读:"+f.canRead());
        System.out.println("文件是否可写:"+f.canWrite());
        System.out.println("文件的名字:"+f.getName());
        System.out.println("上级目录:"+f.getParent());
        System.out.println("是否是一个目录:"+f.isDirectory());
        System.out.println("是否是一个文件:"+f.isFile());
        System.out.println("是否隐藏:"+f.isHidden());
        System.out.println("文件的大小:"+f.length());
        System.out.println("是否存在:"+f.exists());
        System.out.println("绝对路径:"+f.getAbsolutePath());
        System.out.println("相对路径:"+f.getPath());
        System.out.println("toString:"+f.toString());

        String[] list = f.list();//字符串
        for (String s : list) {
            System.out.println(s);
        }
        
        File[] fs = f.listFiles();//file对象
        for (File file : fs) {
            System.out.println(file);
        }

    }
}

IO流的分类

2. 一个一个字符 完成文件的复制

package com.msb.io02;

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

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 20:53
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Test01 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:/test.txt");
        FileReader fr = new FileReader(f);
        int n = fr.read();
//        while (n!=-1){
//            System.out.println(n);//单个字符的字节码打印
//            n = fr.read();
//        }
        while (n!=-1){
            System.out.println((char) n);//单个字符的打印
            n = fr.read();
        }

        fr.close();
    }
}

package com.msb.io02;

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

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 20:53
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Test02 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:/test.txt");
        FileReader fr = new FileReader(f);

        // 缓冲数组
        char[] ch = new char[5];
        int len = fr.read(ch);//一次接收到的实际个数  不满足5的情况下

        while (len!=-1){
//            // 方式1 迭代输出
//            for (int i = 0; i < len; i++) {//根据实际长度迭代
//                System.out.println(ch[i]);
//            }
            //方式2 char列表转String
            String str = new String(ch, 0, len);
            System.out.print(str);

            len = fr.read(ch);
        }

        fr.close();
    }
}

输出内容到文件

package com.msb.io02;

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

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 21:06
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Test03 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:demo.txt");

        FileWriter fw = new FileWriter(f);

        String str = "hello 美女";
        for (int i = 0; i < str.length(); i++) {
            fw.write(str.charAt(i));
        }



        fw.close();

    }
}

文件复制

package com.msb.io02;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/20 - 09 - 20 - 21:09
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo04 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("d:test.txt");
        File f2 = new File("d:demo.txt");


        FileReader fr = new FileReader(f1);
        FileWriter fw = new FileWriter(f2);

        // 方法一
//        int n = fr.read();
//        while (n!=-1){
//            fw.write(n);
//            n = fr.read();
//        }

        // 方法二
//        char[] ch = new char[5];
//        int len = fr.read(ch);
//        while (len!=-1){
//            fw.write(ch, 0, len);//实际有效长度
//            len = fr.read(ch);//注意这里也要传入ch
//        }
        // 方法三
        char[] ch = new char[5];
        int len = fr.read(ch);
        while (len!=-1){
            String str = new String(ch, 0, len);
            fw.write(str);
            len = fr.read(ch);
        }




        fw.close();
        fr.close();


    }
}

用字节流去处理字符流

package com.msb.io02;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 19:45
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo05 {
    public static void main(String[] args) throws IOException {
        //利用字节流 读取文本
        File f = new File("d:\\test.txt");
        FileInputStream fis = new FileInputStream(f);

        /*
        注意1:
        文件是utf-8存储的 一个英文字符占用1字节 一个中文字符占用3个字节

        注意2
        文本文件建议用字符流去处理


         */
        int n = fis.read();
        while (n!=-1){
            System.out.println(n);
            n = fis.read();
        }

        fis.close();

    }
}


警告:不要用字符流去处理字节流
字符流:.txt .java .c .cpp 这些都是存文本的文件
字节流 非文本的文件:.jpg .mp3 .mp4 ...

3. 字节流

beach.jpg

package com.msb.io02;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 19:56
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo06 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:\\beach.jpg");
        FileInputStream fis = new FileInputStream(f);

        int count = 0;

        int n = fis.read();
        while (n!=-1){
            count++;
            System.out.println(n);
            n = fis.read();
        }
        System.out.println("count="+count);
        fis.close();
    }
}



另一种方式读取

package com.msb.io02;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 19:56
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo07 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:\\beach.jpg");
        FileInputStream fis = new FileInputStream(f);

        // 利用缓存数组
        byte[] b = new byte[1024*2];

        // read
        int len = fis.read(b); // 读的时候需要传入b
        while (len!=-1){

            for (int i = 0; i < len; i++) {
                System.out.println(b[i]);
            }

            len = fis.read(b);
        }
        fis.close();
    }
}


图片的复制

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 20:04
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo08 {

    public static void main(String[] args) throws IOException {
        File f1 = new File("d:\\beach.jpg");
        File f2 = new File("d:\\beach_copy.jpg");

        FileInputStream fis = new FileInputStream(f1);
        FileOutputStream fos = new FileOutputStream(f2);

        // copy
        int n = fis.read();

        while (n!=-1){
            fos.write(n);
            n = fis.read();
        }

        fos.close();
        fis.close();


    }
}


再套一层 缓冲字节流

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 20:04
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo09 {

    public static void main(String[] args) throws IOException {
        File f1 = new File("d:\\beach.jpg");
        File f2 = new File("d:\\beach_copy.jpg");

        FileInputStream fis = new FileInputStream(f1);
        FileOutputStream fos = new FileOutputStream(f2);

        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //复制
        byte [] b = new byte[1024*2];
        int len = bis.read(b);
        while (len!=-1){
            bos.write(b, 0, len);//读到多少写多少 长度是len
            len = bis.read(b);
        }


        bos.close();
        bis.close();
        fos.close();
        fis.close();


    }
}


为什么要有缓冲字节流 减少与磁盘的访问次数

同样的也有对应的处理 字符流的 缓冲 方式
BufferedReader BufferedWriter

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 20:42
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo10 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("d:\\test.txt");
        File f2 = new File("d:\\test_copy.txt");

        FileReader fr = new FileReader(f1);
        FileWriter fw = new FileWriter(f2);

        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(fw);

        /*
        int n = br.read();
        while (n!=-1){
            bw.write(n);
            n=br.read();
        }
        */

        /*
        char[] ch = new char[30];
        int len = br.read(ch);
        while (len!=-1){
            bw.write(ch, 0, len);
            len = br.read();
        }

         */

        String str = br.readLine();
        while (str!=null){
            bw.write(str);
            bw.newLine();
            str = br.readLine();
        }



        bw.close();
        br.close();

        fw.close();
        fr.close();

    }
}

4. 转换字节流

InputStreamReader
OutputStreamWriter
文本的底层存储是二进制 可以用 字节流 根据文件的编码格式 转换成 字符流
编码格式 就很重了 否则会转换失败 得到字符出现乱码

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 20:54
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo11 {

    public static void main(String[] args) throws IOException {
        File f = new File("d:\\test.txt");
        FileInputStream fis = new FileInputStream(f);
        InputStreamReader isr= new InputStreamReader(fis, "utf-8");//默认就是utf-8 可以不写

        char[] ch = new char[20];
        int len = isr.read(ch);
        while (len!=-1){
            System.out.println(new String(ch, 0, len));
            len = isr.read(ch);
        }

        isr.close();



    }
}

完成文本的复制

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 21:07
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo12 {
    public static void main(String[] args) throws IOException {
        File f1 = new File("d:\\test.txt");
        File f2 = new File("d:\\test_copy.txt");

        FileInputStream fis = new FileInputStream(f1);
        FileOutputStream fos = new FileOutputStream(f2);

        InputStreamReader isr= new InputStreamReader(fis, "utf-8");//默认就是utf-8 可以不写
        OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");

        char[] ch = new char[20];
        int len = isr.read(ch);
        while (len!=-1){
            osw.write(ch, 0, len);
            len = isr.read(ch);
        }
        
        osw.close();
        isr.close();
    }
}

5. System.in

package com.msb.io02;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 21:14
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo3 {

    public static void main(String[] args) throws IOException {
        InputStream in = System.in;//系统的标准输入 从键盘输入

        int i = in.read();//阻塞方法 等待从键盘输入 得到输入的 ascii码值
        System.out.println(i);
    }
}

直接读取文本的内容并打印

package com.msb.io02;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 21:14
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo3 {

    public static void main(String[] args) throws IOException {
        InputStream in = System.in;//系统的标准输入 从键盘输入

        /*
        int i = in.read();//阻塞方法 等待从键盘输入 得到输入的 ascii码值
        System.out.println(i);
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        System.out.println(i);

         */

        Scanner sc = new Scanner(new FileInputStream(new File("d:\\demo.txt")));
        while (sc.hasNext()){
            System.out.println(sc.next());
        }

    }
}

练习:将键盘的输入内容 写到文本中

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 21:28
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo13 {
    public static void main(String[] args) throws IOException {
        InputStream is = System.in;//字节流
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        File f = new File("d:\\demo1.txt");
        FileWriter fw = new FileWriter(f);
        BufferedWriter bw = new BufferedWriter(fw);

        String s = br.readLine();
        while(!s.equals("exit")){
            bw.write(s);
            bw.newLine();
            s = br.readLine();
        }

        bw.close();
        br.close();
    }
}

7.基本数据类型的数据

DataInputStream:将文件中存储的基本数据类型和字符串 写入 内存的变量中
DataOutputStream: 将内存中的基本数据类型和字符串的变量 写出 文件中

将基本数据类型写到文本中
DataOutputStream

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 21:58
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo14 {
    public static void main(String[] args) throws IOException {
        File f = new File("d:\\demo02.txt");
        FileOutputStream fos = new FileOutputStream(f);
        DataOutputStream dos = new DataOutputStream(fos);

        dos.writeUTF("你好");
        dos.writeBoolean(false);
        dos.writeInt(10086);

        dos.close();


    }
}

同样的方式读入

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 22:03
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo15 {
    public static void main(String[] args) throws IOException {
        DataInputStream dis = new DataInputStream(new FileInputStream(new File("d:\\demo02.txt")));

        System.out.println(dis.readUTF());
        System.out.println(dis.readBoolean());
        System.out.println(dis.readInt());



    }
}

8. object的处理

ObjectInputStream,ObjectInputStream
object 如何冷冻起来 又如何解冻
序列化 -->冷冻
以及反序列化 -->解冻

字符串 序列化--冷冻

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 22:08
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo16 {
    public static void main(String[] args) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:\\demo03.txt")));
        oos.writeObject("你好 china");
        oos.close();
    }
}

字符串反序列化--解冻

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 22:11
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo7 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:\\demo03.txt")));
        String s = (String)(ois.readObject());
        System.out.println(s);

        ois.close();

    }
}

对象的序列化

package com.msb.io02;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 22:13
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Person {
    private String name;
    private int 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;
    }

    public Person() {
    }

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

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 22:14
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Test05 {
    public static void main(String[] args) throws IOException {
        Person p = new Person("lili", 18);
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:\\Demo4.txt")));
        oos.writeObject(p);
        oos.close();

    }
}

报错了什么原因?

对象需要序列化的话 必须实现一个接口 Serializable (可以序列化的) 例如 手机是 可以拍照的

如何反序列化Person object

package com.msb.io02;

import java.io.*;

/**
 * @Auther: jack.chen
 * @Date: 2023/9/21 - 09 - 21 - 22:20
 * @Description: com.msb.io02
 * @version: 1.0
 */
public class Demo17 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:\\Demo4.txt")));

        Person p = (Person)(ois.readObject());
        System.out.println(p.toString());

        ois.close();

    }
}

什么是serialVersionUID?
Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性的。在进行反序列化时,JVM会把传来的字节流中的serialVersionUID与本地相应实体类的serialVersionUID进行比较,如果相同就认为是一致的,可以进行反序列化,否则就会出现序列化版本不一致的异常。(InvalidCastException)

static,transient修饰的属性 不可以被序列化

posted @ 2023-09-21 19:52  jack-chen666  阅读(2)  评论(0编辑  收藏  举报