InputStreamReader和BufferedReader的区别
.InputStream、OutputStream
处理字节流的抽象类
InputStream 是字节输入流的所有类的超类,一般我们使用它的子类,如FileInputStream等.
OutputStream是字节输出流的所有类的超类,一般我们使用它的子类,如FileOutputStream等.
2.InputStreamReader OutputStreamWriter
处理字符流的抽象类
InputStreamReader 是字节流通向字符流的桥梁,它将字节流转换为字符流.
OutputStreamWriter是字符流通向字节流的桥梁,它将字符流转换为字节流.
3.BufferedReader BufferedWriter
BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,readLine读取一个文本行,
从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。
BufferedWriter 由Writer 类扩展而来,提供通用的缓冲方式文本写入, newLine使用平台自己的行分隔符,
将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
InputStream能从來源处读取一個一個byte,
所以它是最低级的,
例:
import <a href="http://lib.csdn.net/base/javase" class='replace_word' title="Java SE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.io.*;
public class Main {
public static void main(String[] args) { try {
FileInputStream fis=new FileInputStream("d://desktop//test.txt");
int i; try {
while((i=fis.read()) != -1){
System.out.println(i);
}
/*假設test.txt檔裡頭只有一個文字 "陳" ,且其編碼為 utf8
* 輸出
* 233
153
179
*/
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
InputStreamReader
InputStreamReader封裝了InputStream在里头,
它以较高级的方式,一次读取一个一个字符,
下例是假设有一个编码为utf8的文档,
其內容只有一个中文字 "陳"
import java.io.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException { FileInputStream fis=new FileInputStream("d://desktop//test.txt");
try {
InputStreamReader isr=new InputStreamReader(fis,"utf8");
int i;
while((i=isr.read()) != -1){
System.out.println((char)i); //輸出 陳
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
BufferedReader
BufferedReader则是比InputStreamReader更高级,
它封裝了StreamReader类,
一次读取取一行的字符
import java.io.*;
public class Main {
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException { FileInputStream fis=new FileInputStream("d://desktop//test.txt"); try {
InputStreamReader isr=new InputStreamReader(fis,"utf8");
BufferedReader br=new BufferedReader(isr); String line;
while((line=br.readLine()) != null){
System.out.println(line);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
}
最新文章
- 开源镜像源(转自[tanghuimin0713的博客])
- json数组转数组对象
- 使用VirtualBox自带管理工具命令为虚拟磁盘扩展空间
- Windows系统镜像自动添加驱动程序
- 【二叉树遍历模版】前序遍历&;&;中序遍历&;&;后序遍历&;&;层次遍历&;&;Root->;Right->;Left遍历
- Android & Eclipse FAQ
- leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)
- How to compile pycrypto 2.4.1 (python 3.2.2 for Windows 7 x64)
- 【转】Java删除文件夹和文件
- StateListDrawable 资源
- APPcache
- Code Complete
- [编织消息框架][JAVA核心技术]动态代理应用11-水平扩展实现
- DocX开源WORD操作组件的学习系列三
- MySQL数据库的锁机制
- windows下查看特定端口被什么程序占用
- mysql: [Warning] Using a password on the command line interface can be insecure. ERROR 1045 (28000): Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)
- 对象存储到session中
- No.10_分数分配
- 编写了几个Java类,但是一直运行某一个class,这种是因为:main方法写错
热门文章
- PostgreSQL恢复误操作
- WebForms UnobtrusiveValidationMode 需要“jQuery”ScriptResourceMapping。
- Zookeeper集群是如何升级到新版本的
- iOS开发之Documentation.build/Script-BC552B3A15.sh:
- get传输时,会将加号+ 转换为空格
- 在ubuntu16.04上安装php7 mysql5.7 nginx1.10并支持http2
- Luogu 2530 化工厂装箱员
- Java Platform SE binary已停止运行 Can't load AMD 64-bit.dll on a IA 32-bit platform错误
- SqlServer 数据库/数据表 拆分(分布式)【转】
- fn project k8s 集成