IO流(2)、原理及流的分类

文章详细介绍了Java中的IO流原理,包括输入/输出的概念、流的分类,如字节流和字符流、输入流和输出流、节点流和处理流。并给出了字节输入流InputStream的使用示例,如FileInputStream的单字节读取和字节数组读取,以及字节输出流OutputStream的写入操作。最后,文章演示了如何使用FileInputStream和FileOutputStream实现文件拷贝。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、IO流原理及流的分类

        1、IO流原理

        a、I/O是 Input / Output的缩写,I/O技术是非常实用的技术,用于处理数据传输。如读/写文件,网络通讯等。

        b、Java程序中,对于数据的输入/输出操作以 "流(stream)"的方式进行。

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

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

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

        2、流的分类

        a、按操作数据单位不同分为:字节流(二进制文件),字符类(文本文件)

        b、按数据流的流向不同分为:输入流、输出流

        c、按流的角色的不同分为:节点流,处理流/包装流

        3、IO流体系图

二、IO流体系图-常用的类

        1、InputStream:字节输入流

        a、FileInputStream

        * InputStream:字节输入流

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

        * InputStream常用的子类

                * 1.FileInputStream:文件输入流

                * 2.BufferInputStream:缓冲字节输入流

                * 3.ObjectInputStream:对象字节输入流

1.一个字节的读
@Test
public void test(){
    String filePath = "E:\\mine\\hello.txt";
    int readLen;
    FileInputStream fileInputStream = null;
    try {
        // 创建FileInputStream对象用于读取文件
        fileInputStream = new FileInputStream(filePath);
        while ((readLen = fileInputStream.read()) != -1){
            System.out.print((char)readLen);
        }
    } catch (IOException e){
        e.printStackTrace();
    } finally {
        try {
            assert fileInputStream != null;
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

        2.字节数组的读

@Test
public void test02(){
    String filePath = "E:\\mine\\hello.txt";
    int readLen;
    byte[] bytes = new byte[8];
    FileInputStream fileInputStream = null;
    try {
        // 创建FileInputStream对象用于读取文件
        fileInputStream = new FileInputStream(filePath);
        while ((readLen = fileInputStream.read(bytes)) != -1){
            System.out.print(new String(bytes,0,readLen));
        }
    } catch (IOException e){
        e.printStackTrace();
    } finally {
        try {
            if (fileInputStream != null){
                fileInputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2、OutputStream:字节输出流

@Test
public void test03(){
    String filePath = "E:\\mine\\a.txt";
    OutputStream outputStream = null;
    try {
        // 创建写入对象 (覆盖)
        outputStream = new FileOutputStream(filePath);
        // 创建写入对象 (不覆盖)
        //outputStream = new FileOutputStream(filePath,true);
        String bytes = "你好,中国";
        // 写入位数
        outputStream.write(bytes.getBytes(StandardCharsets.UTF_8));
        // 写入从off到off+len
        //outputStream.write(bytes.getBytes(),0,3);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null){
                outputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3、文件拷贝

@Test
public void test04(){
    String srcFilePath = "E:\\mine\\居家办公申请书.jpg";
    String destFilePath = "E:\\mine\\居家办公申请书郭策.jpg";

    FileInputStream fileInputStream = null;
    FileOutputStream fileOutputStream = null;

    try {
        // 创建读文件、写文件对象
        fileInputStream = new FileInputStream(srcFilePath);
        fileOutputStream = new FileOutputStream(destFilePath);
        byte[] bytes = new byte[1024];
        int readLen;
        while ((readLen = fileInputStream.read(bytes)) != -1){
            fileOutputStream.write(bytes,0,readLen);
        }
        System.out.println("文件拷贝成功!");
    } catch (IOException e){
        e.printStackTrace();
    } finally {
        try {
            if (fileInputStream != null){
                fileInputStream.close();
            }
            if (fileOutputStream != null){
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
### PyCharm 打开文件显示全的解决方案 当遇到PyCharm打开文件显示全的情况时,可以尝试以下几种方法来解决问题。 #### 方法一:清理缓存并重启IDE 有时IDE内部缓存可能导致文件加载异常。通过清除缓存再启动程序能够有效改善此状况。具体操作路径为`File -> Invalidate Caches / Restart...`,之后按照提示完成相应动作即可[^1]。 #### 方法二:调整编辑器字体设置 如果是因为字体原因造成的内容显示问题,则可以通过修改编辑区内的文字样式来进行修复。进入`Settings/Preferences | Editor | Font`选项卡内更改合适的字号大小以及启用抗锯齿功能等参数配置[^2]。 #### 方法三:检查项目结构配置 对于某些特定场景下的源码视图缺失现象,可能是由于当前工作空间未能正确识别全部模块所引起。此时应该核查Project Structure的Content Roots设定项是否涵盖了整个工程根目录;必要时可手动添加遗漏部分,并保存变更生效[^3]。 ```python # 示例代码用于展示如何获取当前项目的根路径,在实际应用中可根据需求调用该函数辅助排查问题 import os def get_project_root(): current_file = os.path.abspath(__file__) project_dir = os.path.dirname(current_file) while not os.path.exists(os.path.join(project_dir, '.idea')): parent_dir = os.path.dirname(project_dir) if parent_dir == project_dir: break project_dir = parent_dir return project_dir print(f"Current Project Root Directory is {get_project_root()}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只鸟儿

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值