黑马程序员系列第七篇IO(1)

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

asp.net+Android+IOS开发 、Net培训、期待与您交流!

(前言:本篇文章主要依据毕向东老师的课程视频整理而成,如要详细学习,请观看毕老师视频 百度网盘链接地址:http://pan.baidu.com/s/1mgrkJWW)

目录:1、字节流 基类InputStream\OutputStream 2、字符流 基类Reader\Writer

注意:由这四类派生出来的子类名称都是以其父类作为子类名的后缀。

1、字节流 基类InputStream\OutputStream

下图为字节流类体系结构图,红色部分为常用部分

举几个典型例子来更好的了解如何应用

三种以字节流形式读取指定文件输出到控制台的方法(为简便起见,都被封装于各个函数中)

1//读取字节流,单个字符为单位进行2publicstaticvoidreadStream(String filename){34FileInputStream fis=null;5try{6fis=newFileInputStream(filename);7//以read方法的还回值为条件,判断是否读取结束,遇到非char型的就鸡鸡了8intcha;9while((cha=fis.read())!=-1){10System.out.PRintln((char)cha);11}12System.out.println("逐字符方式读取文件内容已结束...");13}catch(FileNotFoundException e) {14e.printStackTrace();15}catch(IOException e) {16e.printStackTrace();17}finally{try{18if(fis!=null)19fis.close();//读取结束,判断若流已被创建,则关闭流资源20}catch(IOException e) {21e.printStackTrace();22}23}24}25//读取字节流,字符数组为单位进行,比较好用26publicstaticvoidreadStream1(String filename){2728FileInputStream fis=null;29try{30fis=newFileInputStream(filename);31//定义每次读取的字符数组大小32byte[] cha=newbyte[1024];33intlen=0;34while((len=fis.read(cha))!=-1){35System.out.println(newString(cha,0,len));36}37System.out.println("逐字符数组读取结束...");38}catch(FileNotFoundException e) {39e.printStackTrace();40}catch(IOException e) {41e.printStackTrace();42}finally{try{43if(fis!=null)44fis.close();//读取结束,判断若流已被创建,则关闭流资源45}catch(IOException e) {46e.printStackTrace();47}48}49}5051//慎用,读取文件太大会出问题。52publicstaticvoidreadStream2(String filename){5354FileInputStream fis=null;55try{56fis=newFileInputStream(filename);57//定义每次读取的字符数组的大小为整个文件全部58byte[] cha=newbyte[fis.available()];59fis.read(cha);60System.out.println(newString(cha));61System.out.println("整篇一次性读取结束...");6263}catch(FileNotFoundException e) {64e.printStackTrace();65}catch(IOException e) {66e.printStackTrace();67}finally{try{68if(fis!=null)69fis.close();70}catch(IOException e) {71e.printStackTrace();72}73}74}

2、字符流 基类Reader\Writer

下图为字节流类体系结构图,红色部分为常用部分

三种以字符流方式读取数据的办法

1//逐字符的原始读取文件中字符,读取的结果打印到控制台2publicstaticvoidreadChar(String filename){34FileReader fr=null;5try{6fr=newFileReader(filename);7//以read函数返回结果来判断是否读完,进行循环8intcha;9while((cha=fr.read())!=-1){10System.out.print((char)cha);11}12}catch(FileNotFoundException e) {13e.printStackTrace();14}catch(IOException e) {15e.printStackTrace();16}finally{17try{18if(fr!=null)19fr.close();//先判断流资源是否存在,然后关闭流资源20}catch(IOException e) {21e.printStackTrace();22}23}24}25//以字符串数组为单位读取数据,结果打印到控制台26publicstaticvoidreadChars(String filename){2728FileReader fr=null;29try{30fr=newFileReader(filename);31//定义字符数组的大小32char[] chars=newchar[1024];33intnumber;34while((number=fr.read(chars))!=-1){35//将字符数组转换成字符串,打印结果36System.out.println(newString(chars,0,number));37}38}catch(FileNotFoundException e) {39e.printStackTrace();40}catch(IOException e) {41e.printStackTrace();42}finally{43try{44if(fr!=null)45fr.close();//先判断流资源是否存在,然后关闭流资源46}catch(IOException e) {47e.printStackTrace();48}49}50}51//字符操作,以字符串数组为单位读取数据,结果打印到控制台并且将行数打印出。增加了一个缓冲区,提高读取效率。52publicstaticvoidbufferReader(String filename){5354LineNumberReader buff=null;55try{56buff=newLineNumberReader(newFileReader(filename));57buff.setLineNumber(0);//指定行号5859String line;60while((line=buff.readLine())!=null){61System.out.println(buff.getLineNumber()+":"+line);//打印带行号的内容62}63}catch(FileNotFoundException e) {64e.printStackTrace();65}catch(IOException e) {66e.printStackTrace();67}finally{68try{69if(buff!=null)70buff.close();//先判断流资源是否存在,然后关闭流资源71}catch(IOException e) {72//FIXME Auto-generated catch block73e.printStackTrace();74}75}76}

两种方式从控制台读取字符存入自定文件中

1//字符操作,读取控制台录入,读到kill行结束操作,保存到filename.txt文件中,以行为单位操作2publicstaticvoidwrite(String filename){3//读取控制台录入4System.out.println("请录入(kill为结束命令,文件保存位置-"+filename+"):");5BufferedReader buff=null;6FileWriter fw=null;7try{8buff =newBufferedReader(newInputStreamReader(System.in));9fw =newFileWriter(filename,true);10//只要读的行数据不为空就循环向文件中写数据11String line;12while((line=buff.readLine())!=null){13if(line.equals("kill"))14

break;15fw.write(line);16}17System.out.println("录入已经结束...");//录入结束提示18}catch(IOException e) {19e.printStackTrace();20}finally{21try{22if(fw!=null)23fw.close();//关闭流资源24}catch(IOException e) {25e.printStackTrace();26}27try{28if(buff!=null)29buff.close();//关闭流资源30}catch(IOException e) {31e.printStackTrace();32}33}34}35//字符操作,读取控制台录入,保存到filename.txt文件中,加了一个缓冲区,提高存储效率36publicstaticvoidbufferWriter(String filename){3738BufferedWriter buffwr=null;39BufferedReader buffre=null;40try{41FileWriter fw=newFileWriter(filename,true);42buffre=newBufferedReader(newInputStreamReader(System.in));43buffwr=newBufferedWriter(fw);44System.out.println("请录入(kill为结束命令,文件保存位置-"+filename+"):");4546String line;47while((line=buffre.readLine())!=null){48if(line.equals("kill"))49

break;50buffwr.write(line);51buffwr.newLine();//将换行符读进保存52}53System.out.println("录入已经结束...");5455}catch(IOException e) {56e.printStackTrace();57}finally{58try{59if(buffwr!=null)60buffwr.close();61}catch(IOException e) {62e.printStackTrace();63}64try{65if(buffre!=null)66buffre.close();67}catch(IOException e) {68e.printStackTrace();69}70}71}

两种文本文件拷贝方式

1//复制文本文件,从from到to2publicstaticvoidcopyTxt(String from,String to){3//定义读写文件4FileReader fr=null;5FileWriter fw=null;67try{8//创建读写文件对象9fr=newFileReader(from);10fw=newFileWriter(to,true);11//定义数组大小12char[] buff=newchar[1024];13intnum;14while((num=fr.read(buff))!=-1){15fw.write(buff, 0, num);16}17System.out.println("文件已经从 "+from+" 文件夹复制到了 "+to+" 文件夹...");18}catch(IOException e) {19e.printStackTrace();20}finally{21try{22if(fr!=null)23fr.close();24}25catch(IOException e) {26e.printStackTrace();27}28try{29if(fw!=null)30fw.close();31}32catch(IOException e) {33e.printStackTrace();34}35}36}37//用装饰类保证一下,提高效率38publicstaticvoidbufferCopyTxt(String from,String to){3940BufferedReader br=null;41BufferedWriter bw=null;42try{43

br=newBufferedReader(newFileReader(from));44bw=newBufferedWriter(newFileWriter(to));4546String line;47while((line=br.readLine())!=null){48bw.write(line);49bw.flush();50}51System.out.println("文件已经从 "+from+" 文件夹复制到了 "+to+" 文件夹...");52}catch(IOException e) {53e.printStackTrace();54}finally{55try{56if(br!=null)57

br.close();58}catch(IOException e) {59//FIXME Auto-generated catch block60e.printStackTrace();61}62try{63if(bw!=null)64

br.close();65}catch(IOException e) {66//FIXME Auto-generated catch block67e.printStackTrace();68}69}70}

复制图片的操作,图片必须以字节流的方式操作

1//复制图片,从from文件到to文件。用到字节流2publicstaticvoidcopyImag(String from,String to){3//定义输入输出字节流4FileInputStream fis=null;5FileOutputStream fos=null;67try{8fis=newFileInputStream(from);9fos=newFileOutputStream(to);10//定义每次复制的单位字符数组大小11byte[] cha=newbyte[1024];12intlen=0;13while((len=fis.read(cha))!=-1){14fos.write(cha,0,len);15}16}catch(FileNotFoundException e) {17e.printStackTrace();18}catch(IOException e) {19e.printStackTrace();20}finally{21try{if(fis!=null)22fis.close();23}catch(IOException e) {24//FIXME Auto-generated catch block25e.printStackTrace();26}27try{if(fis!=null)28fis.close();29}catch(IOException e) {30//FIXME Auto-generated catch block31e.printStackTrace();32}33}34}

初学者难免错误,欢迎评判指教,持续更正ing...........

ASP.Net+Android+IOS开发 、Net培训、期待与您交流!

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航