首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >2021-11-03 杭州 java IO流详解(借鉴-侵-删)

2021-11-03 杭州 java IO流详解(借鉴-侵-删)

作者头像
用户8589624
发布2025-11-13 14:15:54
发布2025-11-13 14:15:54
950
举报
文章被收录于专栏:nginxnginx

IO流简介

1.主要用于文件的读写,数据的网络传输(发送,接收)。 2.流是一组有序的字符集和,是对数据传输的总称或抽象。

分类

1.按照处理数据类型分类:字节流和字符流

  1. 字节流:数据流中最小的数据单元是字节
  2. 字符流:数据流中最小的数据单元是字符

以上为 Java IO流中的四大基流 2.按照流向分类:输入流和输出流 输出:把程序(内存)中的内容输出到磁盘、光盘等存储设备中 输入:读取外部数据(磁盘、光盘等存储设备的数据)到程序(内存)中

使用方法案例

操作 IO 流的模板:   ①、创建源或目标对象     输入:把文件中的数据流向到程序中,此时文件是 源,程序是目标     输出:把程序中的数据流向到文件中,此时文件是目标,程序是源   ②、创建 IO 流对象     输入:创建输入流对象     输出:创建输出流对象   ③、具体的 IO 操作   ④、关闭资源     输入:输入流的 close() 方法     输出:输出流的 close() 方法 注意:1、程序中打开的文件 IO 资源不属于内存里的资源,垃圾回收机制无法回收该资源。如果不关闭该资源,那么磁盘的文件将一直被程序引用着,不能删除也不能更改。所以应该手动调用 close() 方法关闭流资源

代码实例

字节流读操作

代码语言:javascript
复制
package com.guor.javaSE;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
 
public class IOTest {
 
    public static void main(String[] args) {
        test05();
    }
 
    private static final String const_filePath = "D:\\guor\\data\\test.txt";
    private static final String const_filePathChinese = "D:\\guor\\data\\中文.txt";
    private File const_file = null;
    private File const_fileChinese = null;
 
    public IOTest() {
        this.const_file = new File(const_filePath);
        this.const_fileChinese = new File(const_filePathChinese);
    }
 
    /**
     * 字节流读取文件:单个字符读取
     * @param b_chinese_file
     */
    private static void test01(boolean b_chinese_file) {
        IOTest ioTest = new IOTest();
        FileInputStream fis = null;
        try {
            if(true == b_chinese_file) {
                //测试字节流读取中文乱码问题
                fis = new FileInputStream(ioTest.const_fileChinese);
            }else {
                fis = new FileInputStream(ioTest.const_file);
            }
            int read = 0;
            while ((read = fis.read())!=-1) {
                log((char)read, false);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
 
    /**
     * 字节流读取文件:数组循环读取
     */
    private static byte[] test02() {
        IOTest ioTest = new IOTest();
        FileInputStream fis = null;
        int len = 512;
        byte[] buffer = new byte[len];
        try {
            fis = new FileInputStream(ioTest.const_file);
            int read;
            while ((read = fis.read(buffer,0,len)) != -1) {
                log(buffer + "", true, false);
            }
            for(byte b : buffer) {
                if(true == Character.isLetterOrDigit((char)b) || (char)b == '\n') {
                    log((char)b, false, false);
                }
            }
        } catch (FileNotFoundException e) {
            return new byte[1];
        } catch (IOException e) {
            return new byte[1];
        } finally {
            if(fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return buffer;
    }
}

字符流读操作

代码语言:javascript
复制
/**
 * 字符流读取中文文档,解决字节流读取中文乱码问题
 */
private static void test03() {
    IOTest ioTest = new IOTest();
    FileReader fr = null;
    try {
        fr = new FileReader(ioTest.const_fileChinese);
        int read = 0;
        while ((read = fr.read()) != -1) {
            log((char)read, false);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

字节流写操作

代码语言:javascript
复制
/**
 * 字节流写操作
 * @throws IOException
 * @throws FileNotFoundException
 */
private static void test04() {
    String outPath = "D:\\guor\\data\\testNew.txt";
    FileOutputStream fos = null;
    try {
        File file = new File(outPath);
        byte[] buffer = test02();
        fos = new FileOutputStream(file);
        fos.write(buffer);
    } catch (FileNotFoundException e) {
        log("FileNotFoundException: " + e);
    } catch (IOException e) {
        log("IOException: " + e);
    } finally {
        if(fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

字符流写操作

代码语言:javascript
复制
/**
 * 字符流写操作
 * @throws IOException
 * @throws FileNotFoundException
 */
private static void test05() {
    String outPath = "D:\\guor\\data\\中文New.txt";
    IOTest ioTest = new IOTest();
    FileReader fr = null;
    FileWriter fw = null;
    try {
        fr = new FileReader(ioTest.const_fileChinese);
        StringBuilder sb = new StringBuilder();
 
        int read = 0;
        while ((read = fr.read()) != -1) {
            log((char)read, false);
            sb.append((char)read);
        }
 
        File file = new File(outPath);
        fw = new FileWriter(file);
        fw.write(sb.toString());
    } catch (FileNotFoundException e) {
        log("FileNotFoundException: " + e);
    } catch (IOException e) {
        log("IOException: " + e);
    } finally {
        if(fw != null) {
            try {
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(fr != null) {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-06-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • IO流简介
  • 分类
  • 使用方法案例
  • 代码实例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档