首页 > 其他 > 详细

黑马程序员JAVA篇---IO总结

时间:2014-02-05 12:14:42      阅读:346      评论:0      收藏:0      [点我收藏+]

1.字符流专门处理字符数据,文本数据   |FileWriter        |write(字符,字符串,数组)写出的方法        |close()关闭流资源关闭之前刷新  public class FileWriterDemo{   public static void main(String[] args){    FileWriter fw = null;    FileWriter fw1 = null;     try{     fw = new FileWriter("d:\\demo.txt");     fw1 = new FileWriter("d:\\demo1.txt");     fw.Write("qqq");     fw.flush();

    fw1.write("sss");     fw1.flsh();    }catch(IOException e){     e.printStackTrace();    }finally{     try{      if(fw!=null){       fw.close();      }catch(IOException e){       e.printStackTrace();      }     }try{      if(fw1!=null){       fw1.close();      }catch(IOException e){       e.printStackTrace();      }    }   }  }   |FileReader       |read() 读取单个字符返回int值       |读取到文件的结尾返回-1       |--close() 关闭读取流  public class ReaderWriterDemo1{   public static void main(String[] args){    FileReader fr = null;    try{//读取单个的字符  int read()   //read()方法特点:执行一次,就读一个字符,自动向后读名,读到末尾返回-1   //既然每次读取都是用read方法,可以采用循环来读,循环的次数不知道的   //能不能控制循环 呢,我就知道末尾结果是-1     fr = new FileReader("");     int x = 0;     while((x = fr.read())!=-1){      syo((char)x);     }         }catch(IOException e){

    e.printStackTrace();    }finally{     try{      if(fr!=null);      fr.close();     }catch(IOException e){      e.printStackTrace();     }    }   }  }

//第二种读取方式 效率高  

 public class ReaderWriterDemo2{  public static void main(Stirng[] args){   FileReader fr = null;   try{    fr = new FileWriter("d:\\demo");    int x = 0;    char[] ch = new char[1024];    while((x = fr.read(ch))!=-1){     syso(new String(ch,0,x))    }

  }catch(IOException e){    e.printStackTrace();   }finally{    try{     if(fr.read()!=null)     fr.close();    }catch(IOException e){     e.printStackTrace();    }   }  } } 2.字符流的缓冲对象   |构造方法 传递一个字符串输出流 BufferedWriter bfw = new BufferedWriter(Writer out)     |BufferedRriter  字符输入流的缓冲区对象 提高字符输入流的操作效率     |自己的方法 readLine() 读取文本一行返回字符串,没有换行符号,在结尾返回null  import java.io.*;  public class BufferedReaderDemo{   public ststic void main(String[] args)throws IOException{    FileReader fr = new FileReader("d:\\demo");    BufferReader bfr = new BufferedReader(fr);    String line = null;     while((line = bfr.readLine())!=null){

    syso(line);    }    bfr.close();   }   }  3.复制文本文件案例,并且处理异常  字符流只能处理文本文件   |--第一种方法,读一个写一个,效率较低,一般不用  import java.io.*;  public class Copy{   public ststic void main(String[] args)throws IOException{    FileReader fr = null;    FileWriter fw = null;    try{     fr = new FileReader("d:\\demo.txt");     fw = new FileWriter("d:\\copydemo.txt");     int x = 0;     while((x = fr.read())!=-1){      fw.write(x);      fw.flush();     }catch(IOException e){      e.printStackTrace();     }finally{      try{       if(fw!=null)       fw.close();            }catch(IOException e){       e.printStackTrace();      }      try{       if(fr!=null)       fr.close();            }catch(IOException e){       e.printStackTrace();      }     }    }   } 

 }   |--第二种方法 一次读取一批用数组  常用  import java.io.*;  public class Copy{   public ststic void main(String[] args){    FileReader fr = null;    FileWriter fw = null;    try{     fr = new FileReader("d:\\demo.txt");     fw = new FileWriter("d:\\copydemo.txt");     int length = 0;     char[] ch = new char[1024];     while((length = fr.read(ch))!=-1){      fw.write(ch,0,length);      fw.flush();     }    }catch(IOException e){     e.printStackTrace();    }finally{     try{       if(fw!=null)       fw.close();            }catch(IOException e){       e.printStackTrace();      }      try{       if(fr!=null)       fr.close();            }catch(IOException e){       e.printStackTrace();      }    }   }  }

  |装饰类,增强原有对象的功能      //模拟一个readLine方法  //读取需要read类中的read()方法读取  import java.io.*;  public class MyReadLineDemo{   //定义Reader对象   private Reader r;   //构造方法,对Reader对象进行赋值操作传递的是Reader类型的子类对象   public MyReadLineDemo(Reader r){    this.r = r;    }  //开始模拟readLine()   public String myReadLine(){    //定义一个字符串缓冲区    StringBuilder sb = new StringBuilder();    int length = 0;    while((length = r.read())!= -1){     //读取后的字符直接赋值给了length     if(length == ‘\r‘)      continue;     if(length == ‘\n‘)      return sb.toString();   //如果两次if都不成立说明有效字符,存储到缓冲区     sb.append((char)legnth);    }    if(sb.length()!=0)     return sb.toString();    return null;   }   public void myClose()throw IOException{    r.close;   }  }

5.流的使用规律     |选择对象的时候,首先确定操作数据源是什么  |非文本   FileIntputStream 读取操作   BufferedInputStream 用数组效率高  |再次确定操作的数据目的是什么  |非文本   FileOutputStream写出操作   BufferedOutputStream 用数组提高效率  |-控制台或者另一台主机 字节流 OutputStream 文本信息用转换流OutputStreamWriter,字符字节,不确定为文本用字节流

黑马程序员JAVA篇---IO总结

原文:http://www.cnblogs.com/zc332750/p/3538117.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!