zoukankan      html  css  js  c++  java
  • I/O的一些简单操作

                                I  / O

    流(stream)是供数据传输的通道

    1. 字节流(通用的)

    inputStream(父类)

      FileInputStream

    outputStream(父类)

      FileOutputStream

    操作流的步骤

    a)     产生字节流对象

    b)     调用读写方法

    c)     关闭流

    2. public void copyImage(){

    1. 3.    FileInputStream fis=null;
    2. 4.    FileOutputStream fos=null;
    3. 5.    int i=0;
    4. 6.    try {
    5. 7.       //创建流对象
    6. 8.       fis=new FileInputStream("E:/PIC/11080901168c233be5b9173bc7.jpg");
    7. 9.       fos=new FileOutputStream("E:/LM.jpg");
    8.         //读取并写入
    9.         byte[] bt=new byte[1024];
    10.         while((i=fis.read(bt))!=-1){
    11.            fos.write(bt,0,i);
    12.         }
    13.       } catch (FileNotFoundException e) {
    14.         // TODO Auto-generated catch block
    15.         e.printStackTrace();
    16.       } catch (IOException e) {
    17.         // TODO Auto-generated catch block
    18.         e.printStackTrace();
    19.       }finally{
    20.         try {
    21.            fis.close();
    22.            fos.close();
    23.         } catch (IOException e) {
    24.            // TODO Auto-generated catch block
    25.            e.printStackTrace();
    26.         }
    27.        
    28.       }
    29.    }
    30.    public static void main(String[] args) {
    31.       // TODO Auto-generated method stub
    32.       TestIO tio=new TestIO();
    33.       tio.copyImage();

    36. }

    1. 字符流

    a)     父类

                            i.          Reader

    1. 子类    filereader

                           ii.          Writer

    1. 子类   filewriter

    b)     ReadLine 读取一行是bufferedreader中的方法

    c)     流链接

    1. public void buffRead(){
    2.       FileReader fr=null;
    3.       FileWriter fw=null;
    4.       BufferedReader br=null;
    5.       try {
    6.         //创建流
    7.         fr=new
      FileReader("E:/PIC/1.txt");
    8.         fw=new FileWriter("E:/新建文本文档 .doc");
    9.         br=new BufferedReader(fr);
    10.         String str=null;
    11.         //读取
    12.         while((str=br.readLine())!=null){
    13.            fw.write(str+" ");
    14. //         System.out.println(str);
    15.         }
    16.       } catch (FileNotFoundException e) {
    17.         // TODO Auto-generated catch block
    18.         e.printStackTrace();
    19.       } catch (IOException e) {
    20.         // TODO Auto-generated catch block
    21.         e.printStackTrace();
    22.       }finally{
    23.         try {
    24.            fr.close();
    25.            fw.close();
    26.            br.close();
    27.         } catch (IOException e) {
    28.            // TODO Auto-generated catch block
    29.            e.printStackTrace();
    30.         }
    31.       }
    32.    }
    33.    public static void main(String[] args) {
    34.       // TODO Auto-generated method stub
    35.       TestIO tio=new TestIO();
    36. //    tio.copyImage();
    37.       tio.buffRead();

    74. }

         //字节转换字符

    public void inputStreamRead(){

          FileInputStream fis=null;//字节

          BufferedReader br=null;//字符

          try {

            fis=new FileInputStream("E:/PIC/1.txt");

           br=new BufferedReader(new InputStreamReader(fis));//InputStreamReader将字节流转换成字符流

            String str=null;

            while((str=br.readLine())!=null){

               System.out.println(str);

            }

          } catch (FileNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

          } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

          }

       }

       public static void main(String[] args) {

          // TODO Auto-generated method stub

          TestIO tio=new TestIO();

    //    tio.copyImage();

    //    tio.buffRead();

          tio.inputStreamRead();

    }

    1. 对象流

    对象流的子类1. ObjectOutputStream 2. ObjectInputStream

    把内存(堆)中的对象转换成二进制数据输出,叫对象的序列化

    把二进制数据还原成内存当中的对象,叫对象的反序列化(产生对象的第二种方法,第一种使用关键字 new)

    Serializable是标识性接口 (允许可序列化的标识)

    让一个类可序列化必须时间标识接口

    当一个类的属性值不想被序列化时可以属性前面加上transient 关键字

          public void testSeriallzable(){

            ObjectOutputStream oos=null;

            FileOutputStream fos=null;

            TestBean tb=new TestBean();

            tb.setAge(19);

            tb.setName("张三");

           

            try {

               fos=new FileOutputStream("date.sql");

               oos=new ObjectOutputStream(fos);

               oos.writeObject(tb);

            } catch (FileNotFoundException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

            } catch (IOException e) {

               // TODO Auto-generated catch block

               e.printStackTrace();

            }finally{

               try {

                  fos.close();

               } catch (IOException e) {

                  // TODO Auto-generated catch block

                  e.printStackTrace();

               }

            }

          }

      

      

          public static void main(String[] args) {

            // TODO Auto-generated method stub

            TestIO tio = new TestIO();

            // tio.copyImage();

            // tio.buffRead();

            // tio.inputStreamRead();

    //      tio.filedelete("E:/testfile");

            tio.testSeriallzable();

          }

    File 类(文件和目录路径名的抽象表示)

          //递归

       public void filedelete(String path) {

          File file = new File(path);

          File[] fileSize = file.listFiles();

          for (int i = 0; i < fileSize.length; i++) {

            if (fileSize[i].isFile()) {

               fileSize[i].delete();

            } else {

               filedelete(fileSize[i].getAbsolutePath());

            }

          }

          file.delete();

       }

       public static void main(String[] args) {

          // TODO Auto-generated method stub

          TestIO tio = new TestIO();

          // tio.copyImage();

          // tio.buffRead();

          // tio.inputStreamRead();

          tio.filedelete("E:/testfile");

       }

  • 相关阅读:
    ES6变量的解构赋值、字符串的新增方法
    JavaScript的基础语法及DOM元素和事件
    ES 新特性、异步、TypeScript
    JS实现PC端URL跳转到对应移动端URL
    jquery版本过低安全漏洞问题
    重磅!华为云社区·CSDN【寻找黑马程序员】有奖征文活动奖项公布!!
    车标知识学习网页开发,与Flask通过base64展示二进制图片 #华为云·寻找黑马程序员#
    大型情感剧集Selenium:3_元素定位 #华为云·寻找黑马程序员#
    大型情感剧集Selenium:2_options设置 #华为云·寻找黑马程序员#
    【nodejs原理&源码赏析(9)】用node-ssh实现轻量级自动化部署
  • 原文地址:https://www.cnblogs.com/-try/p/3714926.html
Copyright © 2011-2022 走看看