Java中的字符流与字节流
2023-09-14 09:13:18 时间
Java中的字符流与字节流
0.引言
在应用中,经常要完成字符的一段文本输出或读取,用字节流可以吗?为什么需要字符流?
1.计算机中的一切最终都是二进制的字节形式存在。
- 01.写入时,首先读到的是字符,需要得到其对应的字节,然后将字节写入到输出流。
- step 02.读取时,首先读到的是字节,可是我们要把它显示为字符,我们需要将字节转换成字符。
例如:对于 “中国”这两个字符。在写入时,需要将其得到相应的字节,然后写入到输出流;在读取时,将相应的字节流得到字符流,然后通过字符流输出。
由于这样的需求很广泛,所以又专门提供了一个对字节流的包装类——字符流。字符流直接接受字符串,它内部将字符串转换成字节,再写入底层设备。
2.字符向字节转换时,要注意编码的问题。
因为字符串转成字节数组,其实是转成该字符的某种编码的字节形式。
同样,由字节流转换成字符流,也有编码形式的存在。【因为你需要将这些字节编码成相应的字符】
3.字节流【InputStream OutputStream】
- InputStream 类
/**
This abstract class is the superclass of all classes representing an input stream of bytes.
这个抽象类是所有字节输入流的超类
Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input.
应用程序需要定义一个InputStream的子类,这个子类必须提供一个方法,该方法用于返回输入的下一个字节。
@author Arthur van Hoff
@see java.io.BufferedInputStream
@see java.io.ByteArrayInputStream
@see java.io.DataInputStream
@see java.io.FilterInputStream
@see java.io.InputStream#read()
@see java.io.OutputStream
@see java.io.PushbackInputStream
@since JDK1.0
*/
public abstract class InputStream implements Closeable {...}
4.字符流【InputStreamReader OutputStreamWriter】
- FileReader
public class FileReader
extends InputStreamReader
Convenience class for reading character files. The constructors of this class assume that the
default character encoding and the default byte-buffer size are appropriate. To specify these
values yourself, construct an InputStreamReader on a FileInputStream.
FileReader is meant for reading streams of characters. For reading streams of raw bytes,
consider using a FileInputStream.
Since:
JDK1.1
See Also:
InputStreamReader, FileInputStream
译注:
(1)类FileReader继承自InputStreamReader
(2)FileReader是一个读取字符文件的方便类。这个类的构造器假设默认的字符编码和默认的字节缓冲大小是合适的。如果你想自己制定这些值,构造一个InputStreamReader在一个FileInputStream
(3)FileReader是用来阅读字符流的。如果想阅读字节流,请考虑使用FileInputStream
- InputStreamReader类
/**
An InputStreamReader is a bridge from byte streams to character streams: It
reads bytes and decodes them into characters using a specified
java.nio.charset.Charset charset. The charset that it uses
may be specified by name or may be given explicitly, or the platform's
default charset may be accepted.
一个InputStreamReader对象是一个字节流到字符流的桥梁:
它读取字节并使用指定的java.nio.charset.Charset 字符对象将其解码为字符。
被使用的字符可能通过name指定或者被显式指出,或者平台的默认使用字符。
<p> Each invocation of one of an InputStreamReader's read() methods may
cause one or more bytes to be read from the underlying byte-input stream.
To enable the efficient conversion of bytes to characters, more bytes may
be read ahead from the underlying stream than are necessary to satisfy the
current read operation.
<p> For top efficiency, consider wrapping an InputStreamReader within a
BufferedReader. For example:
<pre>
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
</pre>
@see BufferedReader
@see InputStream
@see java.nio.charset.Charset
@author Mark Reinhold
@since JDK1.1
*/
public class InputStreamReader extends Reader {...}
相关文章
- java -version 问题 : C:ProgramDataOracleJavajavapath;
- Java IO--字节-字符流转换OutputStreamWriter/InputStreamReader
- Java IO--字节流与字符流OutputStream/InputStream/Writer/Reader
- java: InputStreamReader将字节的输入流变成字符的输入流,OutputStreamWriter将字符的输出流变成字节的输出流
- java:Writer/Reader字符流操作
- Java实现 LeetCode 567 字符串的排列(滑动窗口,处理区间内的字符数量)
- Java实现 LeetCode 451 根据字符出现频率排序
- Java实现 蓝桥杯 算法训练 p1103
- Java 缩放图片工具类,创建缩略图、伸缩图片比例
- 【JAVA】Java 异常中e的getMessage()和toString()方法的异同
- [Java Spring JWT] JWT unit testing
- Java 及 Kotlin 中的可变参数 vararg
- 【JAVA】Java 异常中e的getMessage()和toString()方法的异同
- 【JAVA】java编译错误:编码UTF8/GBK的不可映射字符
- [Linux] Install java and add JAVA_HOME, PATH
- 架构选型之Nodejs与Java
- Java基础,字节字符
- Interview:Java岗位面试—面试求职攻略之一个JAVA程序员面试心得(非常值得收藏)
- 吃透Java IO:字节流、字符流、缓冲流
- 【华为OD机试 2023】 任务总执行时长(C++ Java JavaScript Python)
- Java的位运算符具体解释实例——与(&)、非(~)、或(|)、异或(^)
- java.lang.OutOfMemoryError: Java heap space错误及处理办法(收集整理、转)
- Java如何从数组中查找公共的元素?
- 第十三届蓝桥杯省赛 Java B 组 C题——字符统计(AC)
- 华为校招机试 - 指令排序(Java & JS & Python)
- 【java】Java 继承
- Java核心类库之(字符集/编码集、File类、递归、IO流:字节流、字符流、特殊操作流)