/*
* 1.字符和字节的关系?
* 字节(Byte):字节是通过网络传输信息(或在硬盘或内存中存储信息)的单位,1个字节等于8个二进制位,是一个很具体的储存空间。
字符:人们使用的记号,抽象意义上的一个符号。 '1', '中', 'a', '$', '¥', ……
* 它们完全不是一个位面的概念,在不同编码里,字符和字节的对应关系不同:
①ASCII码中,一个英文字符(不分大小写)占一个字节的空间,一个中文汉字占两个字节的空间。
②UTF-8编码中,一个英文字符等于一个字节,一个中文(含繁体)等于三个字节。
③Unicode编码中,一个英文等于两个字节,一个中文(含繁体)等于两个字节。
* 2.字符流读取字节文件丢失数据的原因
*/
public class Demo1 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("d:\\a.txt");
FileOutputStream fos = new FileOutputStream("co.txt");
//method_1(fis, fos);
method_2(fis, fos);
//关闭资源
fis.close();
fos.close();
}
//一次读写一个字节数组
private static void method_2(FileInputStream fis, FileOutputStream fos)
throws IOException {
byte[] bytes = new byte[1024];
int len;//每次读取的字节个数
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
}
//一次读写一个字节
private static void method_1(FileInputStream fis, FileOutputStream fos)
throws IOException {
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
}
}
IO流中字节流的回顾
最新推荐文章于 2025-05-21 10:19:44 发布