栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA学习第13天,IO流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA学习第13天,IO流

流的分类

1.按照方向分类:输入流;输出流
2按照操作的单位分类;字节流,字符流
3字节输入流,字节输出流 字节输入流,字节输出流

1.字节输入流
1.抽象父级 InputStream ;—不能实例化

普通子级  FileInputStream--操作文件的字节输入流
构造方法参数;  File file / String pathname    //file 的对象  或者是  String类型的地址值

 普通子级  BufferedInputStream  高效字节输入流
 构造方法参数:   InputStream  但是无法实例化 所以创建的是 FileInputStream 
private static void method1() {      //高效方法
        //创建变量
         InputStream  in=null;
         try{
             //创建io流对象
             in=new BufferedInputStream(new FileInputStream(new File("F:\ready\1.txt")));
             in=new BufferedInputStream(new FileInputStream("F:\ready\1.txt"));

             //使用io流
             int b;
             while((b=in.read())!=-1){
                 System.out.println(b);
             }
          }catch(Exception e){
             e.printStackTrace();

         }finally {
             try {
                 in.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
    }
private static void method2() {    //普通方法
        //定义变量   因为是局部变量 所以需要手动赋值
        InputStream in = null;
        //因为io流容易出现问题 所以用try --catch

            //定义流的对象
            try {
                //分两种方式定义  一种是 file 对象来指定    另一种是  String类型路径来指定
                in = new FileInputStream("F:\ready\1.txt");
                in = new FileInputStream(new File("F:\ready\1.txt"));
                //读取
                //定义一个变量
                int b; //从输入流中读取一个数据字节  返回值是int  所以用int 来接受
                while ((b = in.read()) != -1) {
                    System.out.println(b);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
  }

字节输出流 OutputStream

1抽象父级 OutputStream
普通子级

FileOutputStream --操作文件的字节输出流
构造方法参数 :File file / String pathname
默认存在一个参数append ,默认值为False 也就是覆盖输出,需要手动改成true 可以实现追加输出的效果

BufferedOutputStream–高效字节输出流
构造方法参数 OutputStream 不能被实例化,所以使用子级FileOutputStream.

 private static void method1() { //普通方法
        //1定义局内变量  并赋值
        OutputStream out=null;

        try {
            //创建IO输出流对象
          //  out=new FileOutputStream(new File("F:\ready\1.txt"));//覆盖
           // out=new FileOutputStream("F:\ready\1.txt");//覆盖
            out=new FileOutputStream(new File("F:\ready\1.txt"),true);//拼接
            out=new FileOutputStream("F:\ready\1.txt",true);//拼接

            //使用IO流
            out.write(97);
            out.write(97);
            out.write(97);
            out.write(97);

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
private static void method2() {//高效方法
        //1定义局内变量  并赋值
        OutputStream out=null;

        try{
            //定义IO流的对象
            out=new BufferedOutputStream(new FileOutputStream(new File("F:\ready\1.txt")));//覆盖
            out=new BufferedOutputStream(new FileOutputStream("F:\ready\1.txt"));//覆盖
            out=new BufferedOutputStream(new FileOutputStream(new File("F:\ready\1.txt"),true));//拼接
            out=new BufferedOutputStream(new FileOutputStream("F:\ready\1.txt",true));//拼接

            //使用IO流
            out.write(99);
            out.write(99);
            out.write(99);
            out.write(99);


        }catch(Exception e){

            e.printStackTrace();

        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
    }

字符输入流Reader
1抽象父级Reader
2普通子级
FileReader --操作文件的字符输入流
构造方法参数 :File file String filename
BufferedReader -高效字符输入流
构造方法参数:Reader,但无法创建父级对象,所有用的是FileReader

private static void method2() {  **Reader的普通方法*
        //创造局内变量  并赋值
        Reader in=null;
        //创造IO流对象
        try {
            in=new FileReader("F:\ready\1.txt");
            in=new FileReader(new File("F:\ready\1.txt"));
            //使用IO流对象
            int b;
            while((b=in.read())!=-1){
                System.out.println(b);
             }
         } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                //关流
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
     }

private static void method1() {   
        Reader in=null;
        try{
            //创建IO对象
            in=new BufferedReader(new FileReader(new File("F:\ready\1.txt")));
            in=new BufferedReader(new FileReader(("F:\ready\1.txt")));
            //使用IO
            int b;
            while((b=in.read())!=-1){}
            System.out.println(b);
          }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } 

字符输出流
1抽象父级 writer
普通子级
Filewriter --操作文件的字符输出流
构造方法 File file String filename
默认存在了一个参数 boolean append ,默认值 false ,也就是覆盖输出
如果将FileWrite 第二个参数 append 设置成true ,会变成追加输出的结果 ;

private static void method1() {  //普通方法
        Writer out =null;
        //创建IO流的对象
        try {
              out=new FileWriter(new File("F:\ready\1.txt"));//覆盖
              out=new FileWriter(("F:\ready\1.txt"));//覆盖
              out=new FileWriter(new File("F:\ready\1.txt"),true);//拼接
              out=new FileWriter(("F:\ready\1.txt"),true);//拼接
            //使用IO流的对象
            out.write(99);
            out.write(97);
            out.write(99);
            out.write(97);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
private static void method2() {   //Writer的高效方法
        //定义一个局部变量 ,并赋值
        Writer out =null;
        //创建IO流的对象
        try{
             out=new BufferedWriter(new FileWriter(new File("F:\ready\1.txt")));//覆盖
             out=new BufferedWriter(new FileWriter("F:\ready\1.txt"));//覆盖
          //  out=new BufferedWriter(new FileWriter(new File("F:\ready\1.txt"),true));//追加
          //  out=new BufferedWriter(new FileWriter("F:\ready\1.txt",true));//追加

            //使用IO流对象
            out.write(99);
            out.write(99);
            out.write(99);
            out.write(99);

        }catch(Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

  }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/672026.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号