标签:public new 复习 管道 internet import buffered nts adl
一、什么是流
它代表了有能力产出数据的对象或者有能力接受数据的对象
java类库的I/O由输入输出两部分组成
二、 inputStream InputStreamReader Read的关系
1、inputStream类型
作用:是用来表示那些不同数据源产出的数据输入的类
数据源可以是字节数组 String 对象 文件 管道 internet等
2Reader与inputStream区别
Reader与inputStream最大的区别就是Reader 面向字符 inputStream面向字节
如果要从字节转字符就得有个适配器,所谓适配器举个例子 你去美国旅游你带的手机充电器是220V 而美国的标准电压是110V你要在你的充电器上面套一个东西来适应美国电压 这就是适配器
设计模式中有适配器模式 而InputStreamReader就是这个适配器也应用了适配器模式
2 InputStreamReader与StringBuffer读取文件内容区别
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Review {
static String file = "C:\\practice\\think in java\\src\\2121.txt";
public static void main(String[] args) throws IOException {
String s;
StringBuffer sb=new StringBuffer();
File f =new File(file);
try {
BufferedReader br =new BufferedReader(new FileReader(file));
while((s=br.readLine())!=null)
sb.append(s);
System.out.println(sb.toString());
} catch (FileNotFoundException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
这是BufferedReader去取文件
public class Review2 {
static String file = "C:\\practice\\think in java\\src\\2121.txt";
public static void main(String[] args) throws FileNotFoundException {
FileInputStream is=new FileInputStream(file);
InputStreamReader ir=new InputStreamReader(is);
int c;
try {
while((c=ir.read())!=-1){
System.out.print((char)c);
}
} catch (IOException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
}
}
这是InputStreamReader的
可以看出来两者代码是有区别的
本质上是 InputStreamReader是直接读取内存一个字符一个字符读取
BufferedReader是从缓冲区读取
标签:public new 复习 管道 internet import buffered nts adl
原文地址:http://www.cnblogs.com/katoumegumi/p/7657144.html