IO复习

来源:互联网 发布:学而思当美工工资高吗 编辑:程序博客网 时间:2024/06/08 02:02

              一.  IO简介

                  input    输入    读

                  output   输出   写

            我们可以用IO来创建文件,运用缓冲流复制文件,递归,序列化及反序列化,字符流和字符流读写

 

          1.文件的创建

              (1)实例化File

              (2)判断有没有该文件:exists();

              (3)若存没有该文件,创建文件,再写出该文件

                        <1>创建文件:createNewFile();

                        <2>实例化一个写的方法,放入写的内容:FileWriter

              (4)若存在该文件,就写出该文件

                         <1>实例化一个读的方法:FileReader

                         <2>用char[]将读取到的内容装起来:char ch[]=new char[1024];

                         <4>定义一个结束的int类型:int len=0;

                         <3>读取的内容从开始到结束:

                                        if((len=fr.read(ch))!=-1){
                                                       String str=new String(ch, 0, len);
                                                       System.out.println(str);
                                                                              }


                    2.运用缓冲流复制文件

                         (1)复制谁: File file=new File("放入读取的文件路径”);

                         (2)复制到哪里去: File fileOut=new File("放入存入的路径");

                         (3)运用字符缓冲输入流读取文件:

                                               FileInputStream    fis= new FileInputStream(file)

                                               BufferedInputStream bis=new BufferedInputStream(fis);

                         (4)字符缓冲输出流写出文件:

                                                 FileOutputStream fos=new FileOutputStream(fileOut+"\\"+file.getName());
                                                 BufferedOutputStream bos=new BufferedOutputStream(fos);

                                                  int len=0;
                                                  byte[] by=new byte[1024];
                                                 //获取系统时间
                                                       long before=System.currentTimeMillis();
                                                         if((len=bis.read(by))!=-1){
                                                             bos.write(by, 0, len);
                                                             bis.close();
                                                              bos.close();
                                                             long after=System.currentTimeMillis();
                                                           System.err.println("复制Ok"+(after-before));
                                                                       }


                    3.递归(递归简单点来说就是自己调用自己)

                       运用IO可以实现: 列出文件夹下所有的目录

              


                       4.序列化及反序列化

                             




                           



                            5.字符流和字符流读写