您的位置:首页 > 其它

字节流和字符流的区别

2016-07-27 14:46 375 查看
如果数据流中最小的数据单元是字节,那么称这种流为字节流;如果数据流中最小的数据单元是字符,那么称这种流为字符流

1. 字节流(byte)

- InputStream、OutputStream

- FileInputStream、FileOutputStream

示例代码:

/**

* 字节流 写操作

* @throws FileNotFoundException

*/

public static void byteWriter() throws FileNotFoundException{

File f=new File("g:"+File.separator+"test.txt");

OutputStream out=new FileOutputStream(f);

String s="test...";

byte[] bytes=s.getBytes();

try {

out.write(bytes);

out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


2. 字符流(char)

- Reader、Writer

附:实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件,如图12-6所示。原文链接

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  字符流