IO 流



一、File 类

1.1 作用

能够新建、删除、重命名等操作文件和目录,但是不访问文件内容本身

1.2 构造器

  • public File(String pathName)
    以 pathName 为路径创建 File 对象,可以是绝对路径或者相对路径,如果 pathName 是相对路径,则默认的当前路径在系统属性 user.dir 中存储。
  • public File(String parent,String child)
    以 parent 为父路径,child 为子路径创建 File 对象。

1.3 常用方法

  • 访问文件名:
    getName()
    getPath()
    getAbsoluteFile()
    getAbsolutePath()
    getParent()
    toPath()
    renameTo(File newName)
  • 文件检测
    exists()
    canWrite()
    canRead()
    isFile()
    isDirectory()
  • 文件操作相关
    createNewFile()
    delete()
  • 获取常规文件信息
    lastModified():获取最后一次修改的时间,毫秒
    length()
  • 目录操作相关
    mkdir():只能创建一层目录,其上层目录必须存在
    mkdirs():可以创建多层目录,其上层目录可以不存在
    delete()
    list()
    listFiles()

二、IO 流

2.1 IO 流分类

  • 按操作数据单位不同分为:字节流(8 bit),字符流(16 bit)
    字节流:用于读取非文本文件,以 InputStream 和 OutputStream 结尾
    字符流:用于读取文本文件,以 Reader 和 Writer 结尾
  • 按数据流的流向不同分为:输入流,输出流
    输入流:以 InputStream 和 Reader 结尾
    输出流:以 OutputStream 和 Writer 结尾
  • 按流的角色的不同分为:节点流,处理流
    节点流:直接作用在文件(File)的流,包括:FileInputStream、FileOutputStream、FileReader、FileWriter
    处理流:作用在节点流或处理流之上的流,用于提供更加强大的读写功能,处理流包括:除抽象基类和节点流以外的均为处理流

在这里插入图片描述

2.2 节点流(文件流)

FileInputStream、FileOutputStream、FileReader、FileWriter

使用方式以 FileInputStream 和 FileOutputStream 为例:

public void testFileIOStream(String inputFilePath, String outputFilePath) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        inputStream = new FileInputStream(new File(inputFilePath));
        outputStream = new FileOutputStream(new File(outputFilePath);
        // 用于暂存从输入流中读取的内容
        byte[] buffer = new byte[1024];
        // 每次从输入流中读出的字节长度,若读到文件末尾返回 -1
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
        	// 每次取出 buffer 中 0 ~ len 个长度
            outputStream.write(buffer, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    	// 关闭资源
        try {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

注:FileReader、FileWriter 的使用方式一样

2.3 缓冲流

BufferedInputStream、BufferedOutputStream、BufferedReader、 BufferedWriter

作用:为了提高数据读写的速度,Java API提供了带缓冲功能的流类,在使用这些流类时,会创建一个内部缓冲区数组。

使用方式以 BufferedInputStream 和 BufferedOutputStream 为例:

private void testBufferIOStream(String inputFilePath, String outputFilePath) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
    	// 缓冲流是处理流,需要套接在响应的节点流上使用
        inputStream = new BufferedInputStream(new FileInputStream(new File(inputFilePath)));
        outputStream = new BufferedOutputStream(new FileOutputStream(new File(outputFilePath)));
        byte[] buffer = new byte[1024];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.4 转换流

InputStreamReader、OutputStreamWriter

作用:转换流提供了在字节流和字符流之间的转换,字节流中的数据都是字符时,转成字符流操作更高效。

public void testIORW(String inputFilePath, String outputFilePath) {
    InputStreamReader isr = null;
    OutputStreamWriter osw = null;
    try {
        // 将字节流转换为字符流,并且以 GBK 的方式进行转换
        isr = new InputStreamReader(new FileInputStream(inputFilePath), "GBK");
        // 将字符流转换为字节流,并且以 GBK 的方式进行转换
        osw = new OutputStreamWriter(new FileOutputStream(outputFilePath), "GBK");

        // 因为是操作字符流,所以需要的是 char 字符数组作为缓冲区
        char[] buffer = new char[1024];
        int len;
        while ((len = isr.read(buffer)) != -1) {
            osw.write(buffer, 0, len);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (isr != null) {
                isr.close();
            }
            if (osw != null) {
                osw.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.5 数据流

DataOutputStream、DataInputStream

作用:为了方便地操作Java语言的基本数据类型的数据,可以使用数据流。
注意点:读入的数据要和写入时的顺序相同

常用方法:

  • boolean readBoolean()
  • byte readByte()
  • char readChar()
  • float readFloat()
  • double readDouble()
  • short readShort()
  • long readLong()
  • int readInt()
  • String readUTF()
  • void readFully(byte[] b)
/**
 * 通过数据量写出数据
 */
public void testDataOStream(String outputFilePath) {
    DataOutputStream outputStream = null;
    try {
        outputStream = new DataOutputStream(new FileOutputStream(outputFilePath));

        // 写出字符串
        outputStream.writeUTF("我是数据流");
        outputStream.writeDouble(12.5);
        outputStream.writeInt(15);

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

/**
 * 通过数据量读入数据
 */
public void testDataIOStream(String inputFilePath) {
    DataInputStream inputStream = null;
    try {
        inputStream = new DataInputStream(new FileInputStream(inputFilePath));

        // 读入的数据要和写入的数据顺序相同
        String str = inputStream.readUTF();
        double dou = inputStream.readDouble();
        int num = inputStream.readInt();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2.6 对象流

ObjectInputStream、OjbectOutputSteam

作用:用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。

对象序列化机制
允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象。
在这里插入图片描述
序列化

  • 序列化的好处在于可将任何实现了Serializable接口的对象转化为字节数据,使其在保存和传输时可被还原。
  • 序列化是 RMI(Remote Method Invoke – 远程方法调用)过程的参数和返回值都必须实现的机制,而 RMI 是 JavaEE 的基础。因此序列化机制是 JavaEE 平台的基础。
  • 如果某个类的字段不是基本数据类型或 String 类型,而是另一个引用类型,那么这个引用类型必须是可序列化的,否则拥有该类型的 Field 的类也不能序列化。
  • 不能序列化 static 和 transient 修饰的成员变量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值