• io流


    IO技术:
       将内存中的数据存储到持久性设备上,称为写,是向外output
       将持久性设备里的数据读到内存中,称为读,是向内 input

          流按照方向可以分为输入和输出流      字节流:可以操作任何数据       字符流:只能操作纯字符数据
          outputStream 输出流
          inputStream 输入流
          父类为io


    io程序书写    1、使用前 导包  IO包中的类
                  2、使用前 进行异常的处理
                  3、使用后 释放资源


     字节流:
         
     1、文件输出流:FileoutputStream
         方法:write(byte[] b)   write(byte[],int off,int len)   write(int b)  将指定的数据写到此文件输出流
               close()  关闭流
           public static void main(String[] ages){
                 //创建文件输出流对象   绑定数据目的地
               FileOutputStream file=new FileOutputStream("d:\aaa.txt");
                  //调用write方法  写一个字节
               file.write(97);
                  //写一个字节数组
               byte[] b={78,97,97,67};
                file.write(b);
                  //写一个数组的一部分
               file.write(b,1,2);       write(数组名,起始位置,长度);
                 //写字符串  getBytes()  字符串转字节
                file.write("hello world".getBytes());
                //关闭流
                file.close();
            }
          ////换行( )和续写(true)
              File file=new File("d:\1.txt");
              FileOutputStream fos=new FileOutputStream(file,true);//true以后内容有重复不会覆盖前边的,会向下接着写
              fos.write("hello ".getBytes());
              fos.write("qy97".getBytes());
              fos.close();
     2、文件输入流:FileInputStream
          方法:read()、close()
         字节读取:
          public static void main(String[] args){
             
                        由于数据流会出现异常,就要用try{}...catch(Exception e){e.printStackTrace();}
              try{
                   //创建文件输入流的子类对象
                       FileInputStream file=new FileInputStream("d:\aaa.txt");
                    //调用read()方法
                       int len=0;
                       while((len=file.read()!=-1)){
                           System.out.print((char)len);                                       // read()方法在读取数据时,当读取完后返回-1
                      }                                                                            内容存储到len
       
                  }catch(Exception e){
                           e.printStackTrace();
                       }
               
            }
         字节数组读取:(用数组读取,优点:快)
            public static void main(String[] args){
                    try{
                          FileInputStream file=new FileInputStream("d:\aaa.txt");
                          byte[] b=new byte[1024];
                          int len=0;                                                          //在读取数据时,读取的数据会存入到b数组里边,长度存
                          while((len=file.read())!=-1){                                          储到len,一次读取1024个字节
                              System.out.println(new String(b,0,len));
                              }
                         }catch(Exception e){
                           }
                  }
       复制:
         (1) 读取一个字节,写一个字节
              public static void main(String[] args){
             //建立两个流对象,绑定数据源和目的地
                FileInputStream fis=null;
                FileOutputStream fos=null;
              try{
                   fis=new FileInputStream("d:\aaa.txt");//要读取的文件夹
                   fos=new FileOutputStream("c:\aaa.txt");//要复制进去的文件夹
                   //读取一个字节,写一个字节
                    int len=0;
                    while((len=fis.read())!=-1){
                       fos.write(len);
                           }
                     }catch(Expetion e){
                      System.out.ptintln(e);
                       throe new RuntimeException("文件复制失败");
                      }finally{                                 //关闭资源流,先开后关,后开先关(被复制的后关)
                            if(fos!=null){
                                try{
                                     fos.close();
                                  }catch(Exception e){
                                        e.printStackTrace();
                                     }finally{
                                      if(fis!=null){
                                           try{
                                                fis.close();
                                               }catch(Exception e){
                                                     e.printStackTrace();
                                                      throw new RuntimeException("释放资源");
                                                   }
                                        }
                                       }
                             }
                        }
              }   
       (2)用数组复制文件,优点:快
              public static void main(String[] args){
                    long s=System.currentTimeMillis();//监控程序的运行时间
                     FileInputStream fie=null;
                     FileOutputStream foe=null;
                     try{
                          fie=new FileInputStream("d:\aaa.txt");
                           fos=new FileOutpurStream("c:\aaa.txt");
                             byte[] b=new byte[1024];
                             int len=0;
                             while((len=fis.read(b))!=-1){
                                  fos.write(b,0,len);
                                 }
                           }catch(Exception e){
                                 e.printStackTrace();
                               }finally{
                                   try{
                                        if(fos!=null){
                                              fos.close();
                                               }
                                        }catch(Exception e){
                                           e.printStackTrace();
                                         }finally{ try{
                                        if(fis!=null){
                                              fis.close();
                                               }
                                        }catch(Exception e){
                                           e.printStackTrace();
                                         }
                                }
                       long e=System.currentTimeMillis();
                       System.out.println(e-s);
                   }
               }
           
    字符串流:
       FileWriter   文件输出流
              方法:write()写 、close()关闭流刷新一次缓冲区、flush()刷新缓冲区
              public static void main(String[] args){
                     FileWriter file=new FileWriter("c:\1.txt");
                     file.write(100);
                     file.write(101);
                     char[] c={'a','d','s'};
                     file.write(c);
                     file.write(c,0,len);
                     file.write("Qy97");
                     file.flush();
                     file.close();//字符串流必须必须加flush()和close()后文件才能创建出来
                }


      FileReader    文件输入流
              方法:read()读、close()关闭流刷新一次缓冲区、flush()刷新缓冲区
              public static void main(String[] args){
                     FileReader fr=new FileReader("c:\1.txt");
    \                 int len=0;
    \                while((len=fr.read())!=-1){
    \                    System.out.print((char)len);
    \                   }
                    char[] ch =new char[1024];// 一次读1024个字符
            int len= 0;
            while((len=fileReader.read(ch))!=-1){
                System.out.println(new String(ch,0,len));
            }
            fileReader.close();
                }

    字节转字符
          public static void main(String[] args){
                    writeUTF();
             }
          public static void weiteUTF(){
                try{
                      //字节
                       FileOutputStream fos=new FileOutputStream("c:\utf.txt");
                       OutputStreamWriter sow=new OutputStreamWriter(fos,"utf-8");
                       sow.write("你好");
                       sow.close();
                    }catch(Exception e){
                       e.printStackTrace();
                  }
             }













































  • 相关阅读:
    关于dllimport的使用
    公众平台返回原始数据为: 错误代码-40164
    CentOS7.4 系统下 Tomcat 启动慢解决方法
    PyCharm实现高效远程调试代码
    代码比较工具推荐
    CentOS7 下源码安装 python3
    linux定时任务调度定系统——opencron
    使用 ISO镜像配置 本地yum 源(RHEL, CentOS, Fedora等适用)
    Error: rpmdb open failed
    部署Redis(脚本安装)
  • 原文地址:https://www.cnblogs.com/fbbg/p/10623526.html
Copyright © 2020-2023  润新知