流的分类
按操作数据单位分为:字节流(非文本)和字符流(文本数据) 按数据的流向分为:输入流和输出流 按流的角色分为:节点流和处理流
  
流的使用
- 字节流的读入操作
public class test{
public static void main(String[] args) {
FileReader fr = null;
try {
File file = new File("hello.txt");
fr = new FileReader(file);
int data;
while((data=fr.read())!=-1){
System.out.print((char)data);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fr!=null) fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileReader fr=null;
try {
File file = new File("hello.txt");
fr = new FileReader(file);
char[] cbuf=new char[5];
int len;
while((len=fr.read(cbuf))!=-1){
String s = new String(cbuf, 0, len);
System.out.print(s);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fr!=null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
|