惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
F
Fortinet All Blogs
Cloudbric
Cloudbric
P
Palo Alto Networks Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
AWS News Blog
AWS News Blog
Project Zero
Project Zero
L
LangChain Blog
Cyberwarzone
Cyberwarzone
Engineering at Meta
Engineering at Meta
雷峰网
雷峰网
C
CERT Recently Published Vulnerability Notes
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Security Latest
Security Latest
云风的 BLOG
云风的 BLOG
I
Intezer
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
MongoDB | Blog
MongoDB | Blog
aimingoo的专栏
aimingoo的专栏
K
Kaspersky official blog
Jina AI
Jina AI
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
D
DataBreaches.Net
A
About on SuperTechFans
博客园 - 三生石上(FineUI控件)
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Recorded Future
Recorded Future
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Secure Thoughts
TaoSecurity Blog
TaoSecurity Blog
P
Privacy & Cybersecurity Law Blog
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
IT之家
IT之家
Forbes - Security
Forbes - Security
The Hacker News
The Hacker News
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Y
Y Combinator Blog

博客园 - 有容乃大

GitHub相关 2022又要重新找工作开始新的历程 java之面向对象详解(转) java关于for循环的效率优化 JAVA线程池ThreadPoolExecutor的分析和使用(新手踩坑和推荐方案) JAVA常量池 Java String的intern()注意事项(分JDK1.6及JDK1.7) JAVA的类加载过程 使用RabbitMQ实现延迟任务 JAVA三元运算符空指针引用的坑 Java中static块、构造块、构造函数的执行顺序 Ansj中文分词 关于HashMap、HashSet和ArrayList集合对象容量初始值设置及扩容演示 将异常对象转为字符串 JVM的内存区域划分 深入理解Java String类(综合) 理解Java注解类型 JAVA中Integer的==和equals注意 JVM原理摘要
理解JAVA的IO
有容乃大 · 2019-03-13 · via 博客园 - 有容乃大

1. 什么是流
Java中的流是对字节序列的抽象,我们可以想象有一个水管,只不过现在流动在水管中的不再是水,而是字节序列。和水流一样,Java中的流也具有一个“流动的方向”,通常可以从中读入一个字节序列的对象被称为输入流;能够向其写入一个字节序列的对象被称为输出流

2. 字节流
Java中的字节流处理的最基本单位为单个字节,它通常用来处理二进制数据。Java中最基本的两个字节流类是InputStream和OutputStream,它们分别代表了一组基本的输入字节流和输出字节流。InputStream类与OutputStream类均为抽象类,我们在实际使用中通常使用Java类库中提供的它们的一系列子类。

3. 字符流
Java中的字符流处理的最基本的单元是Unicode码元(大小2字节),它通常用来处理文本数据。所谓Unicode码元,也就是一个Unicode代码单元,范围是0×0000~0xFFFF。在以上范围内的每个数字都与一个字符相对应,Java中的String类型默认就把字符以Unicode规则编码而后存储在内存中。然而与存储在内存中不同,存储在磁盘上的数据通常有着各种各样的编码方式。使用不同的编码方式,相同的字符会有不同的二进制表示。

4. 字符流与字节流的区别
经过以上的描述,我们可以知道字节流与字符流之间主要的区别体现在以下几个方面:
    字节流操作的基本单元为字节;字符流操作的基本单元为Unicode码元。
    字节流默认不使用缓冲区;字符流使用缓冲区。
    字节流通常用于处理二进制数据,实际上它可以处理任意类型的数据,但它不支持直接写入或读取Unicode码元;字符流通常处理文本数据,它支持写入及读取Unicode码元。

示例一:文件读取和写入(支持编码选择、文件内容追加)

/**
* 读取文件文本内容。
*
* @param filePath 文件路径
* @param charset  字符编码格式
* @throws Exception
*/

public static String readAllText(String filePath, String charset) throws Exception {
StringBuilder sb = new StringBuilder();
try (
        InputStreamReader reader = new InputStreamReader(new FileInputStream(filePath), charset);
        BufferedReader br = new BufferedReader(reader)
) {
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line);
        sb.append(SystemCharUtils.getNewLine());
    }
} catch (Exception e) {
    throw e;
}

return sb.toString();
}


/**
* 将字符串追加到文件。
*
* @param str      待写入字符串
* @param filePath 文件路径(文件不存在则自动创建)
* @param charset  字符编码
* @throws Exception
*/
public static void appendFile(String str, String filePath, String charset) throws Exception {
// 创建新文件对象。
File file = new File(filePath);

//自动创建目录。
if (!file.getParentFile().exists()) {
    if (!file.getParentFile().mkdirs()) {
        throw new RuntimeException("文件目录创建失败:" + file.getParentFile().getAbsolutePath());
    }
}

//如果文件不存在则自动创建。
if (!file.exists()) {
    file.createNewFile();
}

try (
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file, true), charset);
        BufferedWriter bw = new BufferedWriter(writer);
) {
    bw.write(str);
} catch (Exception e) {
    throw e;
}
}

示例二:对象序列化到文件流 及 从文件流反序列化为对象
注意:实体对象要实现Serializable接口。

/**
     * 序列化文件路径。
     */
    String localFilePath = SystemCharUtils.getUserDir() + SystemCharUtils.getFileSeparator() + "data" + SystemCharUtils.getFileSeparator() + "test" +SystemCharUtils.getFileSeparator() + "testData.data";

    /**
     * 从文件流反序列化为对象。
     *
     * @param filePath 文件路径
     * @throws Exception
     */
    public static <T> T getObject(String filePath) throws Exception {
        try (
                ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(filePath));
        ) {
            return (T) inputStream.readObject();
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 将对象序列化到文件流。
     *
     * @param obj      需要序列化的对象
     * @param filePath 文件路径
     * @throws Exception
     */
    public static void saveObject(Object obj, String filePath) throws Exception {
        // 创建新文件对象。
        File file = new File(filePath);

        //自动创建目录。
        if (!file.getParentFile().exists()) {
            if (!file.getParentFile().mkdirs()) {
                throw new RuntimeException("文件目录创建失败:" + file.getParentFile().getAbsolutePath());
            }
        }

        //如果文件不存在则自动创建。
        if (!file.exists()) {
            file.createNewFile();
        }

        try (
                ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
        ) {
            outputStream.writeObject(obj);
        } catch (Exception e) {
            throw e;
        }
    }