【JavaSE 第二十天】

【JavaSE 第二十天】

一、 File 类的方法

1. File 类获取的方法

  • File getAbsoluteFile() 获取绝对路径,返回值是 File 类型
  • File getParentFile() 获取父路径,返回值是 File 类型
  • String getName() 获取名字,File 构造方法中的路径的名字
  • String getPath() 获取 File 构造方法中的路径,完整的路径转成 String 返回
  • long length() 获取文件的字节数
/**
 *   File类获取方法
 *   1. String getName() 获取名字,File 构造方法中的路径的名字
 *   2. String getPath() 获取 File 构造方法中的路径,完整的路径转成 String 返回
 */
public static void fileMethod01(){
    File file = new File("C:\\Java\\jdk1.8.0_221\\bin\\java.exe");
    // 1. getName() 获取名字
    String name = file.getName();
    System.out.println("name = " + name); // 看最后一个是什么,它就是什么
    // 2. getPath() 将构造方法中的参数,转成字符串
    String path = file.getPath();
    System.out.println("path = " + path);
}
/**
 *   File 类的获取方法
 *   3. File getAbsoluteFile() 获取绝对路径,返回值是 File 类型
 *   4. File getParentFile() 获取父路径,返回值是 File 类型
 * 	 5. long length() 获取文件的字节数
 */
public static void fileMethod02(){
    File file = new File("C:\\Java\\jdk1.8.0_221\\bin\\java.exe");
    // 3. File getAbsoluteFile() 获取绝对路径
    File absoluteFile = file.getAbsoluteFile();
    System.out.println("absoluteFile = " + absoluteFile); // 如果只写 java.exe 则会默认当前工程下的文件
    // 4. File getParentFile() 获取父路径
    File parentFile = file.getParentFile().getParentFile();
    System.out.println("parentFile = " + parentFile);
    // 5. long length() 获取文件的字节数
    long length = file.length();
    System.out.println("length = " + length);
}

2. File 类的方法 listFiles()

返回值是 File[] 数组,其中存储了多个 File 对象,方法的作用是遍历当前的文件夹

    public static void main(String[] args) {
        fileMethod();
        foreachDir(new File("C:\\Java\\jdk1.8.0_221"));
    }
    /**
     *  遍历目录
     */
    public static void fileMethod(){
        File file = new File("C:\\Java\\jdk1.8.0_221");
        File[] files = file.listFiles();
        for(File f : files){
            System.out.println(f);
        }
    }
    /**
     *  目录的递归遍历:需要传递参数,需要遍历的路径作为参数
     */
    public static void foreachDir(File dir){
        System.out.println(dir);
        // listFiles() 遍历目录  C:\Java\jdk1.8.0_221
        File[] files = dir.listFiles();
        // 遍历数组,取出数组中的 File 对象
        // 遍历到的是所有文件的全路径 (绝对路径)
        for(File f : files){
            // 判断遍历到的路径是不是文件夹
            if(f.isDirectory()) // C:\Java\jdk1.8.0_221\jre ,由于该文件夹下还具有文件,所以进入文件夹继续遍历
                // 递归调用自己,传递路径
                foreachDir(f);
            else
                System.out.println(f);
        }
    }

二、 IO 流对象

IO :(Input Output 输入输出) IO 的作用是将数据从一个设备中流入到另一个设备
比如:一个数据文件,①从磁盘中流向内存中,②从磁盘中流向移动存储设备,③从一台计算机流向另一台计算机

一切数据都是字节:任何数据文件都是由字节组成,字节是计算机中最小的存储单元,不可再次分割 (Java 源码,游戏,音乐,电影等都是由字节构成的)

  • 内存到磁盘中这一过程叫做输出;专业词汇叫做写入硬盘中
  • 磁盘到内存中这一过程叫做输入;专业词汇叫做读取进内存中

1. IO 流对象的分类

(1)按照操作的文件类型分类
  • 文本类型文件 – 选择流对象 字符流
    • 文本文件:使用文本工具:记事本,notepad++ ,editplus 等打开文件后人类可以直接阅读的文件
  • 非文本类型文件 – 选择流对象 字节流
(2)按照数据的流向分类
  • 输入流:Java 程序从其它地方读取数据
  • 输出流:Java 程序中的数据,写入到其它地方
(3)IO 流对象的分类归纳

IO 流四大抽象父类:

  • 字节输出流:OutputStream 抽象类
  • 字节输入流:InputStream 抽象类
  • 字符输出流:Writer 抽象类
  • 字符输入流:Reader 抽象类

三、字节输出流

java.io.OutputStream 是所有字节输出流的超类:可以写入任何类型文件

  • 写入字节的方法 write()
    • void write(int b) 写入单个字节(表面上是 int 类型但是之后自动转换为 byte 类型)
    • void write(byte[] b) 写入字节数组
    • void write(byte[] b,int off,int len) 写入数组的一部分,包括开始索引和写入的个数

1. OutputStream 的子类: FileOutputStream

  • 构造方法:FileOutputStream(File file)
  • 构造方法:FileOutputStream(String file)
    • 两者都是创建字节输出流对象,绑定的参数就是要写入的数据目的(File file 或 String file 叫做目的文件

JVM 很聪明:任何一个操作系统都具备 IO 的能力,JVM 依赖操作系统实现 IO 功能,IO 流对象使用完毕后,一定要要释放资源,否则读写的资源一直被占用。

2. 字节输出流写入文件的步骤

  1. 创建字节输出流对象,构造方法中,绑定文件路径(文件路径就是写入目的)
  2. 调用流对象的方法 write 写入数据
  3. 释放资源

3. 写入单个字节

    /**
     * 写入单个字节
     * new FileOutputStream("c:/1.txt"); 文件如果没有就创建,如果有就进行覆盖
     */
    public static void writeByte() throws IOException {
        // 创建字节输出流对象,构造方法中,绑定文件路径,写入目的
        FileOutputStream fos = new FileOutputStream("c:/1.txt"); // 异常较多,要抛出异常
        // 写入单个字节 不要写超过 127(byte 类型)
        fos.write(45); // 根据 ASCII 表,这是 - (负号)
        fos.write(49); // 根据 ASCII 表,这是 1
        fos.write(48); // 根据 ASCII 表,这是 0
        fos.write(48); // 根据 ASCII 表,这是 0
        // 释放资源
        fos.close();
    }

4. 写入字节数组

    /**
     * 写入字节数组
     */
    public static void writeByteArray() throws IOException {
        // 创建字节输出流对象,构造方法中,绑定文件路径,写入目的
        FileOutputStream fos = new FileOutputStream("c:/1.txt");
        byte[] bytes = {97,98,99,100,101,102}; // 根据 ASCII 表,这是 abcdef
        // 写入字节数组
        fos.write(bytes);
        // 写入字节数组中文
        fos.write("你好,我好,大家好".getBytes()); // 使用方法转为汉字
        // 写入数组一部分
        fos.write(bytes,1,3); // 根据 ASCII 表,这是 bcd
        // 释放资源
        fos.close();
    }

5. 追加写入和换行

  • 追加写入:FileOutputStream 的构造方法 public FileOutputStream(String name,boolean append) 的第二个参数写 true
  • 换行写入:需要使用 Windows 系统的换行符号 \r\n ,Linux 系统中是 \n
    /**
     * 追加写入和换行
     */
    public static void writeAppend()throws IOException {
        // 创建字节输出流对象,构造方法中,绑定文件路径,写入目的
        FileOutputStream fos = new FileOutputStream("c:/1.txt",true);
        fos.write(65); // A
        // 写入换行符号
        fos.write("\r\n".getBytes()); // 使用方法转为字节数组
        fos.write(66); // B
        // 释放资源
        fos.close();
    }

6. IO 异常处理

	// 创建一个流对象
    /**
     *  try catch异常处理:close() 写在 finally
     */
    public static void write01()  {
        // 提升作用域:try 外定义变量,try 创建对象
        FileOutputStream fos = null;
        try {
            // 创建字节输出流对象,构造方法中,绑定文件路径,写入目的
            fos = new FileOutputStream("q:/1.txt");
            // 写入单个字节
            fos.write(45);
            fos.write(49);
            fos.write(48);
            fos.write(48);
        }catch (IOException ex){
            ex.printStackTrace();
        }finally {
            // 释放资源
            try {
                // 流对象创建失败,fos 变量的值是空,不能调用 close
                if(fos != null)
                    fos.close();
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }
    }
    
    // 创建两个流对象
    /**
     *  try catch 异常处理:close() 写在 finally
     */
    public static void write02()  {
        // 因为在不同代码块内,所以需要提升作用域:try 之外定义变量,try 之内创建对象
        FileOutputStream fos = null;
        FileOutputStream fos2 = null;
        try { // 使用 try catch 解决异常
            // 创建字节输出流对象,构造方法中,绑定文件路径,写入目的
            fos = new FileOutputStream("c:/1.txt");
            fos2 = new FileOutputStream("c:/2.txt");
            // 写入单个字节
            fos.write(45);
            fos.write(49);
            fos.write(48);
            fos.write(48);
        }catch (IOException ex){
            ex.printStackTrace();
        }finally {
        
        	// 每个流都需要释放资源
            // 一定要单独释放资源
            try { // 使用 try catch 解决异常
                // 如果流对象创建失败,fos 变量的值是空,那么就不能调用 close
                if(fos != null) // 否则直接释放资源就会发生空指针异常,运行异常
                    fos.close();
            }catch (IOException ex){
                ex.printStackTrace();
            }

            // 一定要单独释放资源
            try {
                // 流对象创建失败,fos 变量的值是空,不能调用 close
                if(fos2 != null)
                    fos2.close();
            }catch (IOException ex){
                ex.printStackTrace();
            }
        }
    }

四、字节输入流

java.io.InputStream 是所有字节输入流的超类:可以读取任何类型文件

  • 读取字节的方法 read()
    • int read() 读取单个字节,读取到流的末尾返回 -1
    • int read(byte[] b) 读取字节数组,读取到流的末尾返回 -1

1. InputStream 的子类: FileInputStream

  • 构造方法 : FileInputStream(File file)
  • 构造方法 : FileInputStream(String file)
    • 两者都是创建字节输入流对象,绑定的参数是要读取的数据源文件(必须要先存在该文件)

2.字节输入流读取单个字节

    /**
     * 字节输入流,读取单个字节
     * int read() 读取单个字节
     */
    public static void readByte()throws IOException {
        // 创建字节输入流对象,绑定数据源文件
        FileInputStream fis = new FileInputStream("c:/1.txt");
        // 读取单个的字节
        // 循环读取,条件 read()!=-1 就行
        int r = 0;
        while ( (r = fis.read()) !=-1){
            System.out.print((char) r);
        }
        // 释放资源
        fis.close();
    }

3. 字节输入流读取字节数组

    /**
     * 字节输入流,读取字节数组
     * int read(byte[] b) 读取字节数组
     * 返回值:返回读取到的字节个数
     * String 类的构造方法 new String(字节数组,开始索引,转换的个数)
     */
    public static void readByteArray()throws IOException{
        // 创建字节输入流对象,绑定数据源文件
        FileInputStream fis = new FileInputStream("c:/1.txt");
        byte[] bytes = new byte[1024]; // 数组推荐是 1024 的整数倍
        // 定义变量,保存 read 方法的返回值
        int r = 0 ;
        while ( (r=fis.read(bytes)) !=-1){
            System.out.print(new String(bytes,0,r)); // 把数组转为字符串
        }
        fis.close();
    }

五、文件复制

实现文件的复制功能,操作系统中的 Ctrl+C,Ctrl+V 一样,原理上就是字节搬家

数据源文件 通过 FileInputStream 读取字节 进入 数据目的文件 通过 FileOutputStream 写入文件,这种方式下需要数组缓冲

    /**
     * 文件复制,就是 IO 流对象的读写
     * 为了提高效率需要使用数组
     */
    public static void copy_1()throws IOException {
        // 字节输入流,绑定数据源文件
        FileInputStream fis = new FileInputStream("c:/1.avi");
        // 字节输出流,绑定要复制的目的文件
        FileOutputStream fos =  new FileOutputStream("e:/1.avi");
        // 字节数组缓冲
        byte[] bytes = new byte[1024];
        // 定义变量,保存读取方法 read 的返回值
        int r = 0;
        // 循环读取数据源文件
        while ( (r=fis.read(bytes)) != -1){
            // 字节输出流,写入字节数组,从0索引开始,写入读取到的个数
            fos.write(bytes,0,r);
        }
        fos.close();
        fis.close();
    }

六、字节流的缓冲流

使用字节流的缓冲流,为了提高原有流对象的读写性能
字节流缓冲区流,流的本质上也是字节流

  • BufferedOuputStream 继承自 OutputStream
    • 方法 write() 写入单个的字节,或者字节数组
  • BufferedInputStream 继承自 InputStream
    • 方法 read() 读取单个字节,或者字节数组

1. BufferedOuputStream 构造方法

BufferedOuputStream (OutputStream out) 传递字节输出流

Buffered 开头的流,称为缓冲流,FileOutputStream 称为基础流

new BufferedOuputStream (new FileOutputStream()) 传递哪个基础流,就对哪个基础流进行高效操作(这种 Buffered 叫做 File 的装饰者,功能加强)

2. BufferedInputStream 构造方法

BufferedInputStream(InputStream in) 传递字节输入流
new BufferedInputStream(new FileInputStream()) 传递基础流,对基础流进行高效操作

    /**
     *  文件复制,缓冲流实现,高效
     */
    public static void copy_2()throws IOException {
        // 字节输入流,绑定数据源文件
        FileInputStream fis = new FileInputStream("c:/1.avi");
        // 字节输出流,绑定要复制的目的文件
        FileOutputStream fos =  new FileOutputStream("e:/1.avi");
        // 创建字节输入流的缓冲流,提高原有字节输入流的效率
        BufferedInputStream bis = new BufferedInputStream(fis); // 传递基础流,提高效率
        // 创建字节输出流的缓冲流,提高原有字节输出流的效率
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        // 字节数组缓冲
        byte[] bytes = new byte[1024];
        // 定义变量,保存读取方法 read 的返回值
        int r = 0;
        while ( (r = bis.read(bytes)) !=-1){
            bos.write(bytes,0,r);
        }
        // 当使用缓冲流的时候,只需要关闭缓冲流即可,基础流会被缓冲流关闭
        bis.close();
        bos.close();
}

当使用缓冲流的时候,只需要关闭缓冲流即可,基础流会被缓冲流关闭

七、字符流

字符流只能操作文本文件

  • Writer 类,是所有字符输出流的父类 (写入文本文件)
    • write(int c) 写入单个字符
    • write(char[] ch) 写入字符数组
    • write(char[] ch,int off,int len) 写入字符数组一部分,开始索引,写入的个数
    • write(String str) 写入字符串
    • void flush() 刷新该流的缓冲 (写入数据,先写入到内存),只有刷新了数据才会到达目的文件
  • Reader 类,是所有字符输入流的父类 (读取文本文件)
    • int read() 读取单个字符
    • int read(char[] ch) 读取字符数组

1. OutputStreamWriter 类

OutputStreamWriter 继承自 Writer,是字符的输出流,同时又是转换流
字符流 = 字节流 + 编码表

OutputStreamWriter 转换流:字符流通向字节流的桥梁,①字符通过编码表转成字节;②之后字节再通过字节流写入文件

  • 构造方法:

    • OutputStreamWriter(OutputStream out) 传递任意字节输出流
    • OutputStreamWriter(OutputStream out,String 编码表名) 传递任意字节输出流
  • 转换流写入字符文件,UTF-8 编码

    /**
     *  使用编码表,UTF-8
     */
     public static void writeUTF8()throws IOException {
         // 创建字节输出流
         FileOutputStream fos = new FileOutputStream("c:/utf.txt");
         // 创建转换流对象,构造方法传递字节的输出流
         OutputStreamWriter osw = new OutputStreamWriter(fos);
         // 写入字符串
         osw.write("你好");
         // 刷新流
         osw.flush();
         // 资源释放
         osw.close();
     }
  • 转换流写入字符文件, GBK 编码
    /**
     * 使用编码表,GBK
     */
    public static void writeGBK()throws IOException{
        // 创建字节输出流
        FileOutputStream fos = new FileOutputStream("c:/gbk.txt");
        // 创建字符输出流,转换流,构造方法传递字节输出流,指定编码表名
        OutputStreamWriter osw = new OutputStreamWriter(fos,"gbk");
        // 写入字符串
        osw.write("你好");
        osw.close();
    }

2. InputStreamReader 类

InputStreamReader 继承自 Reader,字符输入流,读取文本文件,同时又是转换流

InputStreamReader 转换流:字节流通向字符流的桥梁,字节流转成了字符流

  • 构造方法:
    • InputStreamReader(InputStream out) 传递任意字节输入流
    • InputStreamReader(InputStream out,String 编码表名) 传递任意字节输入流
    /**
     * 读取 GBK 文件
     */
    public static void readGBK() throws IOException{
        // 创建字节流对象,绑定数据源
        FileInputStream fis = new FileInputStream("c:/gbk.txt");
        // 创建转换流对象,绑定字节输入流,指定编码表
        InputStreamReader isr = new InputStreamReader(fis,"GBK");
        // 读取字符数组
        char[] chars = new char[1024];
        // 保存读取方法 read 的返回值
        int r = 0 ;
        r = isr.read(chars);
        // 数组转成字符串
        System.out.println(new String(chars,0,r));
        isr.close();
    }

    /**
     * 读取 UTF-8 文件
     */
    public static void readUTF8()throws IOException {
        // 创建字节流对象,绑定数据源
        FileInputStream fis = new FileInputStream("c:/utf.txt");
        // 创建转换流对象,绑定字节输入流
        InputStreamReader isr = new InputStreamReader(fis);
        // 读取字符数组
        char[] chars = new char[1024];
        // 保存读取方法 read 的返回值
        int r = 0 ;
        r = isr.read(chars);
        // 数组转成字符串
        System.out.println(new String(chars,0,r));

        isr.close();
    }

综述

3. 便捷类

  • FileWriter 继承自 OutputStreamWriter
    • 它是字符的输出流,写入文本文件
    • 直接采用默认的编码表(但是改变编码表必须使用父类)
    • FileWriter 的构造方法直接传递字符串的文件名即可
public static void main(String[] args) throws IOException {
    //创建字符输出流的便捷类
    FileWriter fw = new FileWriter("day20/hehe.txt");
    //写入字符串
    fw.write("谢谢");
    fw.close();
}
  • FileReader 继承自 InputStreamReader
    • 它是字符的输入流,读取文本文件
    • 直接采用默认的编码表(但是改变编码表必须使用父类)
    • FileReader 的构造方法直接传递字符串的文件名即可
public static void main(String[] args)throws IOException {
    // 创建字符输入流对象,绑定数据源
    FileReader  fr = new FileReader("day20/hehe.txt");
    // 读取字符数组
    char[] chars = new char[1024];
    int r = 0;
    while ( (r = fr.read(chars)) !=-1){
        System.out.print(new String(chars,0,r));
    }
    fr.close();
}

4. 字符输出流的缓冲流

BufferedWriter:字符流的缓冲流,继承 Writer,写入文本文件

  • 具有一个特殊的方法:newLine() 写入文本换行符,具有平台无关性(System.out.println(); 就是调用了这个方法,具有平台无关性)
  • 构造方法:BufferedWriter(Writer w) 可以传递任意字符输出流
    /**
     * 字符输出流的缓冲区,写入换行
     */
    public static void write() throws IOException {
        // 创建字符输出流的便捷类
        FileWriter fw = new FileWriter("day20/xixi.txt");
        // 创建字符输出流的缓冲流,构造方法传递 fw 流
        BufferedWriter bfw = new BufferedWriter(fw);
        bfw.write("第一行");
        bfw.newLine();
        bfw.write("第二行");
        bfw.flush();
        bfw.close();
    }

5. 字符输入流的缓冲流

BufferedReader:字符流的缓冲流,继承自 Reader,读取文本文件

  • 特殊的方法:String readLine() 读取文本一行,具有平台无关性
  • 构造方法:BufferedReader (Reader r) 可以传递任意字符输入流
    /**
     * 字符输入流的缓冲区,读取文本行
     */
    public static void read() throws IOException {
        // 创建字符输入流的便捷类
        FileReader fr = new FileReader("day20/xixi.txt");
        // 创建字符输入流的缓冲区流对象
        BufferedReader bfr = new BufferedReader(fr);
        // 定义一个字符串,用于保存读取到的文本行
        String line = null;
        while ( (line = bfr.readLine()) != null){
            System.out.println(line);
        }
        bfr.close();
    }

6. 字符流复制文本文件(错误的应用)

这是一个错误的应用,因为它不能保证复制后和源文件是一致的

八、打印流

  • PrintStream:字节输出流
  • PrintWriter:字符输出流
  • 打印流的特性:
    1. 打印流只负责输出打印,不关心数据源
    2. 可以方便的打印各种形式数据
    3. 打印流永远不会抛出 IOException (IO异常)
    4. 具有自动刷新的能力
      自动刷新的条件:构造方法第二个参数写 true,第一个参数必须是 IO 流对象,不能是字符串
/**
 * 打印流输出,在打印流的构造方法中,传递一个流,可以是(字节流或字符流)
 * 自动刷新的条件:构造方法第二个参数写 true,第一个参数必须是 IO 流对象,不能是字符串
 * 调用方法:println、printf、format 三个其中的一个,才能够启用自动刷新
 */
public static void print()throws IOException {
    // 便捷类
    FileWriter fw = new FileWriter("day20/print.txt");
    // 创建打印流对象,传递便捷类
    PrintWriter pw = new PrintWriter(fw,true); 
    pw.println(1.5); // 这是方便打印,原样输出,放入什么打印什么
}

九、基本数据类型流

  • DataInputStream

    • 基本数据类型读取流
    • 构造方法,传递字节输入流
  • DataOutputStream

    • 基本数据类型的写入流
    • 构造方法,传递字节输出流
    public static void main(String[] args) throws IOException {
    	write();
        read();
    }
    
    /**
     *  DataOutputStream 写入基本类型
     */
    public static void write()throws IOException {
        // 创建基本数据类型输出流
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("day20/data.txt"));
        // 写入基本数据类型
        dos.writeInt(6); // 转成二进制,具有四个字节,但是查编码表没有该数值
        dos.writeInt(16);
        dos.writeInt(26);
        dos.writeInt(-1);
        dos.writeInt(100);
        dos.close();
    }
    
    /**
     * DataInputStream 读取基本类型
     * readInt() 读取到文件结束:抛出 EOFException
     */
    public static void read()throws IOException{
        // 创建基本类型输入流,构造方法绑定字节输入流
        DataInputStream dis = new DataInputStream(new FileInputStream("day20/data.txt"));
        // 读取基本类型,采用循环读取
        while (true) { // 必须写死,因为可能有不同的数据类型,并且只能使用异常处理
            try {
                int i = dis.readInt();
                System.out.println("i = " + i);
            }catch (IOException ex){
                // ex.printStackTrace(); 打印异常信息
                break;
            }
        }
        dis.close();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值