.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();
} }
}

最新文章

  1. 开源镜像源(转自[tanghuimin0713的博客])
  2. json数组转数组对象
  3. 使用VirtualBox自带管理工具命令为虚拟磁盘扩展空间
  4. Windows系统镜像自动添加驱动程序
  5. 【二叉树遍历模版】前序遍历&amp;&amp;中序遍历&amp;&amp;后序遍历&amp;&amp;层次遍历&amp;&amp;Root-&gt;Right-&gt;Left遍历
  6. Android & Eclipse FAQ
  7. leetcode@ [126] Word Ladder II (BFS + 层次遍历 + DFS)
  8. How to compile pycrypto 2.4.1 (python 3.2.2 for Windows 7 x64)
  9. 【转】Java删除文件夹和文件
  10. StateListDrawable 资源
  11. APPcache
  12. Code Complete
  13. [编织消息框架][JAVA核心技术]动态代理应用11-水平扩展实现
  14. DocX开源WORD操作组件的学习系列三
  15. MySQL数据库的锁机制
  16. windows下查看特定端口被什么程序占用
  17. 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)
  18. 对象存储到session中
  19. No.10_分数分配
  20. 编写了几个Java类,但是一直运行某一个class,这种是因为:main方法写错

热门文章

  1. PostgreSQL恢复误操作
  2. WebForms UnobtrusiveValidationMode 需要“jQuery”ScriptResourceMapping。
  3. Zookeeper集群是如何升级到新版本的
  4. iOS开发之Documentation.build/Script-BC552B3A15.sh:
  5. get传输时,会将加号+ 转换为空格
  6. 在ubuntu16.04上安装php7 mysql5.7 nginx1.10并支持http2
  7. Luogu 2530 化工厂装箱员
  8. Java Platform SE binary已停止运行 Can't load AMD 64-bit.dll on a IA 32-bit platform错误
  9. SqlServer 数据库/数据表 拆分(分布式)【转】
  10. fn project k8s 集成