java字节流和字符流
java字节流和字符流
JavaIO流,是一种计算机用语。主要是用于处理数据的传输。我们可以把流理解为一种信息传输的媒介。
流的分类
字节传输方式
字符传输方式
输入
输出
输入
输出
IO流
字节流
字符流
InputStream
OutputStream
Reader
Writer
这里列举的类都是顶层的类,没有详细展开,对于各个细分的应用领域会用到不同的类,比如文件流就会用到FileInputStream等,线程间的通信就会用到PipedInputStream等。
有关注意事项:
字符的编码方式有很多种,不同的编码方式字符做占用的字节数也不一样。
写数据时,需要指定编码方式。
读取字节流时需要指定编码方式,读取字符流时可以沿用写数据时的编码方式。
计算机的传输都是字节,字符在传输时都会转换成字节,所以会使用缓冲区。
字节流
这里引用两篇之前的文章,介绍基础的字节流操作–读取。
这里引用两篇之前的文章,介绍基础的字节流操作–写入。
字符流
这里引用两篇之前的文章,介绍基础的字符流操作–读取。
这里引用两篇之前的文章,介绍基础的字符流操作–写入。
字符串转字节数组
String to byte array using getBytes(Charset)
使用该方法将字符串转换成对应编码的字节数组。具体的字符编码可以使用
StandardCharsets.UTF_8类的成员指定。
字节数据转字符串
String transferResult = new String(b, “utf-8”);
切记字符集一定更要申明,当然还有一种方法是通过字符流,字节流的相互转换。
U2FsdGVkX18OcT4CNouD2yBWAhcMdYXu9qP2AK7/DFY7OTkv9PwTkOgjaJltNucf
PRP3yYNQOu6vLq50irdifO81Up0SKAVEgzVJH5PzuVYif3UF9UBBJwrVNxaXim79
VbqruTJ/TeJxjJ9qfI87T5y332Pv5op9pKhCgS2EeUuMjkHRZdPwAE8J9M3kCyXR
qLXLmgg3+9U3P+BvFYtDam0cPflAPe8InJRWzI0eyLTZneeBJL97g2O5Qz0TZPSg
Sxa/R8xl04lCkmfarqbRMSH4amixF4QRx7Qnd/QndYxpv6frxvYa1Vf/GzVCccLs
rqSDsHkhr/L9bFMRGZr2YvGM0WCKsa5EDA2kTK7eDWeNM4spAr2S9Nk6lKDo9LNG
kHslusPiDGt1ALk/VSnvxjqlT5WFwrBFUuVxpKnmDWe9ggR148xT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