Java IO:字节和字符数组

在 Java 中,字节数组和字符数组通常用于在应用程序内部临时存储数据,例如:

InputStream input = new FileInputStream(new File("D:\\input.txt"));
// 缓存读取到数据
byte[] output = new byte[input.available()];
int val, index = 0;
while((val = input.read()) != -1) {
    output[index++] = (byte)val;
}
input.close();
// 将读取的数据转换成字符串,输出到控制台
System.out.println(new String(output, "UTF-8"));

上面示例中,使用 byte[] 数组临时存放 input.txt 文件内容。因此,数组也是 Java IO 到常见数据来源或数据目的地。如果在程序运行过程中需要经常访问文件内容,也可以将文件加载到数组中。

通过 InputStream 或 Reader 读取数组

如果想要让 InputStream 或 Reader 这样的组件从数组中读取数据,则必须将字节或字符数组封装在字节数组输入流(ByteArrayInputStream)或字符数组(CharArrayReader)中。这样,数组中可用的字节或字符就可以通过封装流或 Reader 读取。

下面是一个简单的例子:

package com.hxstrive.java_io.demo01;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * ByteArrayInputStream 示例
 * @author hxstrive.com
 */
public class ByteArrayInputStreamDemo {

    public static void main(String[] args) throws Exception {
        byte[] bytes = "hello world".getBytes();
        // 创建 ByteArrayInputStream, 使用传入的字节数组作为数据源
        InputStream inputStream = new ByteArrayInputStream(bytes);
        int data = inputStream.read();
        while (data != -1) {
            System.out.print((char) data); // hello world
            data = inputStream.read();
        }
    }

}

如果要对字符数组进行同样的操作,与上面示例非常类似。只需将字符数组封装在 CharArrayReader 中就可以了,例如:

package com.hxstrive.java_io.demo01;

import java.io.CharArrayReader;
import java.io.Reader;

/**
 * ByteArrayInputStream 示例
 * @author hxstrive.com
 */
public class CharArrayReaderDemo {

    public static void main(String[] args) throws Exception {
        char[] chars = "hello world".toCharArray();
        // 创建 CharArrayReader, 使用传入的字节数组
        Reader reader = new CharArrayReader(chars);

        int data = reader.read();
        while (data != -1) {
            System.out.print((char) data); // hello world
            data = reader.read();
        }
    }

}

从上面示例中,我们了解了如何将字节或字符数组作为 Java IO 到输入源。

通过 OutputStream 或 Writer 写入数组

当然,也可以将数据写入字节数组输出流(ByteArrayOutputStream)或字符数组写入器(CharArrayWriter)。你只需创建字节数组输出流(ByteArrayOutputStream)或字符数组写入器(CharArrayWriter),然后像写入其他流或写入器一样将数据写入其中。一旦所有数据都写入其中,只需调用 toByteArray() 或 toCharArray 方法,所有写入的数据就会以数组形式返回。

下面是一个简单的例子:

package com.hxstrive.java_io.demo01;

import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;

/**
 * ByteArrayOutputStream 示例
 * @author hxstrive.com
 */
public class ByteArrayOutputStreamDemo {

    public static void main(String[] args) throws Exception {
        // 创建 ByteArrayOutputStream 对象
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        // 写入数据到输出流
        byte[] bytes = "hello world".getBytes();
        outputStream.write(bytes);

        System.out.println(new String(outputStream.toByteArray(), StandardCharsets.UTF_8));
        // 输出:hello world
    }

}

如果要使用字符数组也与本例类似,只需将字符数组封装在 CharArrayWriter 中即可,例如:

package com.hxstrive.java_io.demo01;

import java.io.CharArrayWriter;

/**
 * ByteArrayOutputStream 示例
 * @author hxstrive.com
 */
public class CharArrayWriterDemo {

    public static void main(String[] args) throws Exception {
        // 创建 ByteArrayOutputStream 对象
        CharArrayWriter writer = new CharArrayWriter();

        // 写入数据到输出流
        char[] bytes = "hello world".toCharArray();
        writer.write(bytes);

        System.out.println(new String(writer.toCharArray(), 0, writer.size()));
        // 输出:hello world
    }

}

上述实例中,我们了解了如何将字节流或字符流中到数据写入到字节或字符数组中,关于更多 API 细节将在后续章节介绍。

说说我的看法
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
公众号