转自:http://blog.csdn.net/zhuruoyun/article/details/8174510
I :input,输入 ,O:output,输出 。I/O处理技术是Java语言中实现文件操作、内存操作、控制台输入以及网络编程的基础。
IO类
java.io
为了使输入和输出的结构保持统一,从而方便程序员使用IO相关的类,在Java语言的IO类设计中引入了一个新的概念——Stream(流)。
1、输入流(Input Stream):
该类流将外部数据源的数据转换为流,程序通过读取该类流中的数据,完成对于外部数据源中数据的读入。
2、输出流(Output Stream):
该类流完成将流中的数据转换到对应的数据源中,程序通过向该类流中写入数据,完成将数据写入到对应的外部数据源中。
在java.io包中又实现了两类流:字节流(byte stream)和字符流(char stream)。
这两种流实现的是流中数据序列的单位,在字节流中,数据序列以byte为单位,也就是流中的数据按照一个byte一个byte的顺序实现成流,对于该类流操作的基本单位是一个byte;而对于字节流,数据序列以char为单位,也就是流中的数据按照一个char一个插入的顺序实现成流,对于该类流操作的基本单位是一个char。
字符输入流(Reader体系):
Reader体系中读取数据的基本单位是字符(Char),就是每次至少读取数据一个字符(两个字节)的数据。
体系常用方法:
void close() 关闭该流并释放与之关联的所有资源。
int read() 读取单个字符。
int read(char[] cbuf) 将字符读入数组。
int read(char[] cbuf, int off, int len) 将字符读入数组的某一部分。
int read(CharBuffer target) 试图将字符读入指定的字符缓冲区。
注意的是,创建Reader实例对象,和调用上述方法均会抛出IOException异常。
字符输出流(Writer体系):
Writer体系中的类写入数据的基本单位是字符(char),也就是每次最少写入一个字符(两个字节)的数据。
体系常用方法
Writer append(char c) 将指定字符添加到此 writer。
Writer append(CharSequence csq) 将指定字符序列添加到此 writer。
Writer append(CharSequence csq, int start, int end) 将指定字符序列的子序列添加到此 writer.Appendable。
void close() 关闭此流,并在关闭之前刷新流。
void flush() 刷新该流的缓冲。
void write(char[] cbuf) 写入字符数组。
void write(char[] cbuf, int off, int len) 写入字符数组的某一部分。
void write(int c) 写入单个字符。
void write(String str) 写入字符串。
void write(String str, int off, int len) 写入字符串的某一部分。
练习1:
- import java.io.*;
- public class IODemo1{
- public static void main(String[] args){
- writTest();
- readTest();
- }
- public static void writTest(){
- FileWriter fw = null;//先定义fw的类型,并初始化为null
- try{
- fw=new FileWriter("demo.txt"); //new FileWriter的实例对象
- fw.write("写入啊发把灯关掉非常v字的发的发的噶短发");
- }catch(IOException e){
- throw new RuntimeException("写入错误!");
- }
- //必须执行close方法
- finally{
- //close 也会抛异常,所以需要try catch。
- try{
- //当且仅当fw对象创建成功时才需要关闭资源。
- if(fw !=null)
- fw.close();
- }
- catch(IOException e){}
- }
- }
- public static void readTest(){
- FileReader fr = null;//先定义fr的类型,并初始化为null
- try{
- fr = new FileReader("demo.txt");
- char[] fileBuff = new char[1024];//定义一个字符数组缓冲区,大小为2kB
- int len = 0; //读取的字符长度
- while((len=fr.read(fileBuff))!=-1){ //当read返回 -1时,即代表字符读取完毕。
- System.out.print(new String(fileBuff,0,len));
- }
- }catch(IOException e){
- throw new RuntimeException("读取错误!");
- }finally{
- try{
- //当且仅当fr对象创建成功时才需要关闭资源。
- if(fr !=null)
- fr.close();
- }
- catch(IOException e){}
- }
- }
- }
练习2,复制文本文件:
- import java.io.*;
- public class FileCopyTest{
- public static void main(String[] args){
- copyFile("TestMain6.java","TestMain6.java.txt");
- }
- public static void copyFile(String from,String to){
- FileWriter fw = null;
- FileReader fr = null;
- try{
- fr = new FileReader(from);
- fw = new FileWriter(to);
- char[] buff = new char[1024]; //2kB
- int len = 0;
- while(len!=-1){
- len = fr.read(buff);
- if(len == -1){
- System.out.println("复制结束.");
- break;}
- fw.write(buff,0,len);
- System.out.println("正在复制...");
- }
- } catch(IOException e){
- throw new RuntimeException("读写异常!");
- } finally {
- try {
- if(fr != null)
- fr.close();
- }catch (IOException e){
- throw new RuntimeException("异常!");
- }
- try {
- if(fw != null)
- fw.close();
- }catch (IOException e){
- throw new RuntimeException("异常!");
- }
- }
- }
- }
练习3,模拟BufferedReader:
- class MyBufferedReader extends Reader{ //继承Reader方法
- private Reader r;
- MyBufferedReader(Reader r){ //接受Reader的实例流对象
- this.r = r;
- }
- public String readLine() throws IOException{ //模拟行读取
- int ch =0;
- StringBuilder sb = new StringBuilder(); //用StringBuilder来模拟缓冲
- while((ch=r.read())!=-1){
- if(ch =='\r')
- continue;
- if(ch =='\n') //如果读到换行符,返回一行数据为字符串
- return sb.toString();
- else
- sb.append((char)ch); //将读取的数据转换为char类型,并添加
- }
- if(sb.length()!=0) //考虑到最后一行没有换行符的情况,如果缓冲还有数据则返回
- return sb.toString();
- return null;
- }
- /**
- 必须覆盖Reader类中的抽象方法。
- */
- public int read(char[] cbuf, int off, int len) throws IOException
- {
- return r.read(cbuf,off,len) ;
- }
- public void close()throws IOException{
- r.close();
- }
- }
- public class MyBufferedReaderDemo{
- public static void main(String[] args){
- FileReader fr = null;
- MyBufferedReader myBufReader = null;
- try{
- fr = new FileReader("IODemo.java");
- myBufReader = new MyBufferedReader(fr);
- String line = null; //定义读取的行字符串
- while((line=myBufReader.readLine())!=null){
- System.out.println(line);
- }
- } catch(IOException e){}
- finally{
- try{
- if (myBufReader!=null)
- myBufReader.close();
- } catch(IOException e){}
- }
- }
- }