(十五)IO 流

一、文件(P611)

1. 文件流

文件在程序中是以流的形式来操作的

(1)流:数据在数据源(文件)和程序(内存)之间经历的路径
(2)输入流:数据从数据源(文件)到程序(内存)的路径
(3)输出流:数据从程序(内存)到数据源(文件)的路径

2. 常用的文件操作

(1)new File(String pathname):根据路径构建一个File对象

    public void create01() throws IOException {
        String pathname = "d:\\news1.txt";
        File file = new File(pathname);
        if (file.createNewFile()) {
            System.out.println("创建成功");
        } else {
            System.out.println("创建失败");
        }
    }

(2)new File(File parent,String child):根据父目录文件+子路径构建

    public void create02() throws IOException {
        File parent = new File("d:\\");
        String child = "news2.txt";
        File file = new File(parent, child);
        if (file.createNewFile()) {
            System.out.println("创建成功");
        } else {
            System.out.println("创建失败");
        }
    }

(3)new  File(String parent,String child):根据父目录+子路径构建

    public void create03() throws IOException {
        String parent = "d:\\";
        String child = "news3.txt";
        File file = new File(parent, child);
        if (file.createNewFile()) {
            System.out.println("创建成功");
        } else {
            System.out.println("创建失败");
        }
    }

3. 获取文件的相关信息

    public void info() {
        File file = new File("d:\\news1.txt");

        String name = file.getName(); // 文件名字
        String absolutePath = file.getAbsolutePath(); // 文件绝对路径
        String parent = file.getParent(); // 文件父级目录
        long length = file.length(); // 文件大小(字节)
        boolean exists = file.exists(); // 文件是否存在
        boolean b1 = file.isFile(); // 是不是一个文件
        boolean b2 = file.isDirectory(); // 是不是一个目录
    }

4. 目录的操作和文件删除

    public void m1(){
        File file = new File("d:\\news1.txt");
        if (file.exists()){
            if (file.delete()){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        }else {
            System.out.println("文件或目录不存在");
        }
    }

public void m2(){
        File file = new File("d:\\news1.txt");
        if (file.exists()){
            System.out.println("该文件或目录存在");
        }else {
            // file.mkdir() 创建一级目录
            // file.mkdirs() 创建多级目录
            if (file.mkdirs()){
                System.out.println("该目录创建成功");
            }else {
                System.out.println("该目录创建失败");
            }
        }
    }

二、IO 流

1. IO 流原理

(1)I/O 是 input/output 的缩写,I/O 技术是非常实用的技术,用于处理数据传输。如读/写文件,网络通讯等。

(2)Java程序中,对于数据的输入/输出操作以“流(stream)”的方式进行

(3)java.io 包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过方法输入或输出数据

(4)输入input:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中。

(5)输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中

2. 流的分类

(1)按操作数据单位不同分为:字节流(8bit)【二进制文件】,字符流(按字符)【文本文件】

(2)按数据流的流向不同分为:输入流,输出流

(3)按流的角色的不同分为:节点流,处理流/包装流


(1)Java的 IO 流共涉及40多个类,实际上非常规则,都是从如上4个抽象基类派生的。
(2)由这四个类派生出来的子类名称都是以其父类名作为子类名后缀。

三、InputStream【字节输入流】、OutputStream【字节输出流】

 InputStream 抽象类是所有字节输入流的超类

1. Inputstream常用的子类

(1)FilelnputStream:文件输入流
(2)BufferedlnputStream:缓冲字节输入流
(3)ObjectlnputStream:对象字节输入流

2. 单个字节的读取

// 单个字节的读取
    public void readFile1(){
        String filePath = "d:\\hello.txt";
        FileInputStream fileInputStream = null;
        int readData = 0;

        try {
            fileInputStream = new FileInputStream(filePath);
            while ((readData = fileInputStream.read()) != -1){
                System.out.print((char) readData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

3. 字节数组读取

// 字节数组读取
    public void readFile2() {
        String filePath = "d:\\hello.txt";
        FileInputStream fileInputStream = null;
        byte[] buf = new byte[8];
        int readLen = 0;
        try {
            fileInputStream = new FileInputStream(filePath);
            while ((readLen = fileInputStream.read(buf)) != -1) {
                System.out.println(new String(buf, 0, readLen));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

4. OutputStream

    public void writeFile1() {
        String filePath = "d:\\a.txt";
        FileOutputStream fileOutputStream = null;

        try {
            // 如果为true,则字节将被写入文件的末尾【追加】
            // 如果为false,则字节将被写入文件的开头【覆盖】
            fileOutputStream = new FileOutputStream(filePath,true);

            // 1.写入一个字节
            fileOutputStream.write('Y');

            // 2.写入字符串
            String str = "hello,java";
            fileOutputStream.write(str.getBytes());

            // 3.写入字符串指定范围
            fileOutputStream.write(str.getBytes(), 0, str.length());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

5. 案例 - 文件拷贝

    public void fileCopy(){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        String scrPath = "d:\\ces.jpg";
        String destPath = "d:\\ces1.jpg";
        int readLen = 0;
        byte[] buf = new byte[1024];

        try {
            fis = new FileInputStream(scrPath);
            fos = new FileOutputStream(destPath);

            while ((readLen = fis.read(buf)) != -1) {
                fos.write(buf, 0, readLen);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

四、FileReader 和 FileWriter 

FileReader 和 FileWriter是字符流,即按照字符来操作 IO

1. 单个字符读取

// 单个字符读取
    public void readFile01() {
        String pathname = "d:\\story.txt";
        FileReader fileReader = null;
        int data = 0;
        try {
            fileReader = new FileReader(pathname);
            while ((data = fileReader.read()) != -1) {
                System.out.println((char) data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

2. 字符数组读取

// 字符数组读取
    public void readFile02() {
        String pathname = "d:\\story.txt";
        FileReader fileReader = null;
        int readLen = 0;
        char[] buf = new char[8];
        
        try {
            fileReader = new FileReader(pathname);
            while ((readLen = fileReader.read(buf)) != -1) {
                System.out.println(new String(buf, 0, readLen));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

3. FileWriter 常用方法(写入)

    public void readFile01() {
        String pathname = "d:\\story.txt";
        FileWriter fileWriter = null;
        char[] chars = {'a', 'b', 'c'};
        String str = "你好,java";

        try {
            fileWriter = new FileWriter(pathname, false);

            // 1.写入单个字符
            fileWriter.write('Y');

            // 2.写入指定数组
            fileWriter.write(chars);

            // 3.写入指定数组的指定部分
            fileWriter.write(chars, 0, 1);

            // 4.写入字符串
            fileWriter.write(str);

            // 5.写入字符串的指定部分
            fileWriter.write(str, 0, 1);


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

五、节点流和处理流

 

1. 节点流和处理流的区别和联系

(1)节点流是底层流/低级流,直接跟数据源相接

(2)处理流包装节点流,既可以消除不同节点流的实现差异,也可以提供更方便的方法来完成输入输出

(3)处理流(也叫包装流)对节点流进行包装,使用了修饰器设计模式,不会直接与数据源相连

2. 处理流的功能主要体现在以下两个方面

(1)性能的提高:主要以增加缓冲的方式来提高输入输出的效率

(2)操作的便捷:处理流可能提供了一系列便捷的方法来一次输入输出大批量的数据,使用更加灵活方便

3. BufferdReader

    public static void main(String[] args) throws Exception {
        FileReader fileReader = new FileReader("d:\\hello.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String line ;

        while ((line = bufferedReader.readLine()) != null){
            System.out.println(line);
        }

        bufferedReader.close();
    }

4. BufferdWriter

    public static void main(String[] args) throws Exception {
        FileWriter fileWriter = new FileWriter("d:\\hello.txt",true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        bufferedWriter.write("你好,Java");
        // 换行
        bufferedWriter.newLine();
        bufferedWriter.write("加油");

        bufferedWriter.close();
    }

5. BufferdInputStream BufferdOutputStream

    public static void main(String[] args) throws Exception {

        FileInputStream fis = new FileInputStream("d:\\ces.jpg");
        FileOutputStream fos = new FileOutputStream("d:\\ces2.jpg");

        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedOutputStream bos = new BufferedOutputStream(fos);

        byte[] buf = new byte[1024];
        int readLean = 0;
        while ((readLean = bis.read(buf)) != -1){
            bos.write(buf,0,readLean);
        }

        bis.close();
        bos.close();
    }

6. 对象流 Objectlnputstream 和 ObjectOutputStream

6.1 序列化和反序列化

(1)序列化就是在保存数据时,保存数据的值和数据类型

(2)反序列化就是在恢复数据时,恢复数据的值和数据类型

(3)需要让某个对象支持序列化机制,则必须让其类是可序列化的,为了让某个类是可序列化的,该类必须实现如下两个接口之一:

①Serializable【这是一个标记接口】

②Externalizable

6.2 对象处理流使用细节

    public static void main(String[] args) throws Exception {
        // 序列化后,保存的文件格式是特殊格式
        String filePath = "e:\\data.dat";
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));

        oos.writeInt(100);
        oos.writeBoolean(true);
        oos.writeChar('a');
        oos.writeDouble(9.5);
        oos.writeUTF("你好");

        oos.writeObject(new Dog("旺财"));

    }

    public static void main(String[] args) throws Exception {
        String filePath = "e:\\data.dat";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));

        // 读取(反序列化)顺序和保存(序列化)顺序保持一致
        System.out.println(ois.readInt());
        System.out.println(ois.readBoolean());
        System.out.println(ois.readChar());
        System.out.println(ois.readDouble());
        System.out.println(ois.readUTF());
        System.out.println(ois.readObject());

    }

6.3 注意事项

1)读写顺序要一致
2)要求序列化或反序列化对象,需要实现 Serializable
3)应列化的类中建议添加 SerialVersionUID,为了提高版本的兼容性
4)序列化对象时,默认将里面所有属性都进行序列化,但除了 static 或 transient 修饰的成员
5)序列化对象时,要求里面属性的类型也需要实现序列化接口
6)序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实
现了序列化

7. 标准输入输出流

    public static void main(String[] args) {

        // public final static InputStream in = null;
        // System.in 编译类型 InputStream
        // System.in 运行类型 BufferedInputStream
        System.out.println(System.in.getClass());

        // public final static PrintStream out = null;
        // System.in 编译类型 PrintStream
        // System.in 运行类型 PrintStream
        System.out.println(System.out);

        System.out.println("hello");

        Scanner sc = new Scanner(System.in);
        System.out.println(sc.next());

    }

8. 转换流 InputStreamReader 和 OutputStreamWriter

    public static void main(String[] args) throws Exception {

        method1();
        // 使用转换流解决乱码问题
        method2();
    }

    public static void method1() throws Exception {
        // 文件编码为 gbk
        BufferedReader br = new BufferedReader(new FileReader("e:\\a.txt"));

        String line = br.readLine();
        System.out.println(line); // ���
        br.close();
    }

    public static void method2() throws Exception {
        // 文件编码为 gbk
        InputStreamReader isr = new InputStreamReader(new FileInputStream("e:\\a.txt"), "gbk");
        BufferedReader br = new BufferedReader(isr);

        String line = br.readLine();
        System.out.println(line); // 你好
        br.close();
    }

    public static void main(String[] args) throws Exception {

        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("e:\\ab.txt"), "utf8");
        osw.write("你好");
        BufferedWriter bw = new BufferedWriter(osw);
        bw.write("星期天");
        bw.close();

    }

9. 打印流 PrintStream 和 PrintWriter

    public static void main(String[] args) throws IOException {

        PrintStream out = System.out;
        out.println("join");
        out.write("你好".getBytes());

        out.close();

        // 修改打印流输出位置
        System.setOut(new PrintStream("e:\\f1.txt"));
        System.out.println("你好,java");

    }

    public static void main(String[] args) throws IOException {

//        PrintWriter printWriter = new PrintWriter(System.out);
        PrintWriter printWriter = new PrintWriter(new FileWriter("e:\\f2.txt"));
        printWriter.print("你好,Java!");
        printWriter.close();
    }

六、Properties 类

1. Properties 基本介绍

(1)专门用于读写配置文件的集合类

(2)注意:键值对不需要有空格,值不需要用引号起来。默认类型是String

(3)Properties 的常见方法

2. Properties 的常见方法

(1)load:加载配置文件的键值对到Properties对象
(2)list:将数据显示到指定设备
(3)getProperty(key):根据键获取值

(4)get(key):根据键获取值
(5)setProperty(key,value):设置键值对到Properties对象
(6)store:将 Properties 中的键值对存储到配置文件,在idea中,保存信息到配置文件,如果含有中文,会存储为unicode码

    // 传统方法读取文件
    public static void method1() throws Exception {
        BufferedReader br = new BufferedReader(new FileReader("src\\mysql.properties"));
        String line = "";
        while ((line = br.readLine())!= null){
            String[] split = line.split("=");
            System.out.println(split[0] + "值是:"+split[1]);
        }

        br.close();
    }

    // Properties 类读取文件
    public static void method2() throws Exception {

        Properties properties = new Properties();
        properties.load(new FileReader("src\\mysql.properties"));

        properties.list(System.out);

        System.out.println(properties.get("ip"));
        System.out.println(properties.getProperty("user"));
    }

    // Properties 类添加键值对到文件中
    public static void method3() throws Exception {

        Properties properties = new Properties();

        properties.setProperty("charset","utf8");
        properties.setProperty("user","Jack");
        properties.setProperty("pwd","123");

        // 文件存在,则覆盖旧文件。不存在,新建文件。
        properties.store(new FileWriter("src\\mysql2.properties"),null);

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值