木舟0基础学习Java的第十八天(IO流,字节流,字符流,缓冲)

IO流正常使用流程:1.抛异常  2.资源读写  3.关闭资源(从后往前关)

字节流:(拷贝推荐使用)

开发中一般不会抛出异常 用try{}catch(){} 也不推荐字节流读中文

FileInputStream:读

         FileInputStream fs=new FileInputStream("e:/b.txt");
            //11111111为-1的补码 会认为已经读取完毕 如果用byte 会出现数据读取不完整
            int i=0;
            while((i=fs.read())!=-1){
                System.out.println((char)i);
            }
            //必须关闭资源
            fs.close();
    }

FileOutputStream:写

 public static void main(String[] args) throws Exception {
        //如果e盘下没有b.txt FileOutputStream会自己创建一个
        FileOutputStream fo=new FileOutputStream("e:/b.txt");
        //write 一次写出一个字节
        fo.write(99);//c
        fo.write(100);//d
        fo.close();
        //追加 true代表不覆盖文件中的旧数据 追加到文件末尾
        FileOutputStream fo1=new FileOutputStream("e:/b.txt",true);
        fo1.write(97);
        fo1.write(98);
        fo1.close();
        //连续写入
        FileOutputStream fo2=new FileOutputStream("e:/b.txt");
          String s="abcdefghijklmn";
        //String s="我爱学习";//1f`  字节流写中文会乱码
        for(int i=0;i<s.length();i++){
            char c = s.charAt(i);
            fo2.write(c);
        }
        fo2.close();
    }

读写案例

案例:字节拷贝(读一个字节写一个字节 效率低)
   public static void main(String[] args) throws Exception {
        FileInputStream fs=new FileInputStream("e:/1.jpg");//读
        FileOutputStream out=new FileOutputStream("e:/cba/1.jpg");//写
        int i=0;
        //一边读一边写
        while((i=fs.read())!=-1){//每次读一个字节
            out.write(i);//每次写一个字节
        }
        //先关后面的 再关前面的 关两个
        out.close();
        fs.close();
    }
案例:字节数组拷贝(读总字节写总字节 效率高)

开发中推荐 数组长度byte[] b=new byte[1024*8];

声明一个用来记录读取有效长度的变量 int len=0;

public static void main(String[] args) throws Exception {
        FileInputStream in=new FileInputStream("e:/1.jpg");//读
        FileOutputStream out=new FileOutputStream("e:/cba/1.jpg");//写
        //创建字节数组
        byte[] b=new byte[1024*8];//开发中推荐 数组长度byte[] b=new byte[1024*8];
        int len=0;//声明一个用来记录读取有效长度的变量 int len=0;
        while((len=in.read(b))!=-1){
            System.out.println(len);
            out.write(b,0,len);
        }
        out.close();
        in.close();
    }

 逻辑:

  public static void main(String[] args) throws Exception {
        FileInputStream in=new FileInputStream("e:/1.jpg");//读
        FileOutputStream out=new FileOutputStream("e:/cba/1.jpg");//写
        int available = in.available();//获取文件总字节数
        //创建字节数组
        byte[] b=new byte[available];//创建一个数组 长度是图片所有字节长度
        while(in.read(b)!=-1){
            out.write(b);
        }
        out.close();
        in.close();
    }

Buffered缓冲区:

 public static void main(String[] args) throws Exception {
        FileInputStream in=new FileInputStream("e:/cba/w.mp4");
        BufferedInputStream bis=new BufferedInputStream(in);//自带缓冲区 底层就是new byte[8192]
        FileOutputStream out=new FileOutputStream("e:/abc/w.mp4");
        BufferedOutputStream bos=new BufferedOutputStream(out);//先写到缓冲区 写满后一次性写到文件中
        int b=0;
        while((b=bis.read())!=-1){
            bos.write(b);
        }
        //关闭缓冲区 关闭缓冲区后会自动关闭 in和out
        bos.close();
        bis.close();
    }
数组和Buffered:(开发中推荐数组拷贝)

如果是8192字节大小 数组比Buffered快一些 因为读和写都是同一数组

但是Bufferde操作的是两个数组

关闭资源(close,flush)

close:关闭释放资源 如果是带缓冲区的流对象 不但会关闭流 还会在关闭流之前刷新缓冲区 但是关闭之后不能再写出

flush:用来刷新缓冲区 刷新后可以再写出

开发中标准写法:

 public static void main(String[] args) {
        FileInputStream in=null;
        FileOutputStream out=null;
        try {
          in=new FileInputStream("e:/cba/w.mp4");
          out=new FileOutputStream("e:/abc/ww.mp4");
            int len=0;
            byte[] b=new byte[1024*8];
            while((len=in.read(b))!=-1){
                out.write(b,0,len);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(out!=null){//避免下面报空指针异常
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

案例:用键盘录入数据 写到硬盘  (带缓冲区和不带缓冲区)

public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        FileOutputStream out=null;
        try {
            out=new FileOutputStream("e:/cba/aaa.txt",true);
            while(true){
                System.out.println("请输入:");
                String s=sc.next();
                if(!s.equals("quit")){
                    byte[] b=s.getBytes();
                    out.write(b);
                }else{
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        FileOutputStream out=null;
        BufferedOutputStream bos=null;
        try {
            out=new FileOutputStream("e:/cba/bbb.txt");
            bos=new BufferedOutputStream(out);
            while(true){
                System.out.println("请输入:");
                String s=sc.next();
                if(!s.equals("puit")){
                    byte[] b=s.getBytes();
                    bos.write(b);
                    bos.flush();//刷新缓冲区 及时把缓冲区数据刷入硬盘
                }else{
                    break;
                }
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

字符流:(纯文本时推荐使用)

不可以拷贝非纯文本文件

FileReader:读

 public static void main(String[] args) {
        FileReader r=null;
        try {
            r=new FileReader("e:/cba/a.txt");
            int a=0;
            while((a=r.read())!=-1){
                System.out.println((char)a);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(r!=null){
                try {
                    r.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

FileWrite:写

 public static void main(String[] args) {
        FileWriter w=null;
        try {
            w=new FileWriter("e:/cba/n.txt");
            String s="你好,我不好!";
            char[] c = s.toCharArray();//将字符转为字节存入char数组
            w.write(c);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(w!=null){
                try {
                    w.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

读写案例

案例:字符拷贝(效率低 容易拷贝不完整)
public static void main(String[] args) {
        FileReader fr=null;
        FileWriter fw=null;
        try {
            fr=new FileReader("e:/cba/a.txt");
            fw=new FileWriter("e:/abc/b.txt");
            int a=0;
            while((a=fr.read())!=-1){
                fw.write(a);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            try {
                fw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fr.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
案例:字符数组拷贝(效率高 拷贝完整)
public static void main(String[] args) {
        FileReader fr=null;
        FileWriter fw=null;
        try {
            fr=new FileReader("e:/cba/a.txt");
            fw=new FileWriter("e:/abc/a.txt");
            char[] c=new char[1024*8];
            int len=0;
            while((len=fr.read(c))!=-1){
                fw.write(c,0,len);
                System.out.println(len);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            try {
                fw.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            try {
                fr.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

Buffered缓冲区:

BufferedReader

public static void main(String[] args) {
        FileReader fr=null;
        BufferedReader br=null;
        try {
            fr=new FileReader("e:/abc/a.txt");
            br=new BufferedReader(fr);
            String s=null;
            //br.readLine()方法 可以读取一行字符 但不包括换行符号
            while((s=br.readLine())!=null){
                System.out.println(s);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            try {
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

BufferedWriter

 public static void main(String[] args) {
        FileWriter fw=null;
        BufferedWriter bw=null;
        try {
            fw=new FileWriter("e:/abc/ab.txt");
            bw=new BufferedWriter(fw);
            bw.write("你好");
            bw.write("\r\n");
            bw.write("早上好");
            bw.newLine();//写入一个可以跨平台的换行符 相当于bw.write("\r\n"); 推荐使用!!!
            bw.write("不好!");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            try {
                bw.flush();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

BufferedReader和BufferedWriter

 public static void main(String[] args){
        FileReader fr=null;
        BufferedReader br=null;
        FileWriter fw=null;
        BufferedWriter bw=null;
        try {
            fr=new FileReader("e:/abc/a.txt");
            br=new BufferedReader(fr);
            fw=new FileWriter("e:/cba/t.txt");
            bw=new BufferedWriter(fw);
            String s=null;
            while((s=br.readLine())!=null){
                bw.write(s);
                bw.flush();
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }

将文本反转案例:

 public static void main(String[] args) {
        //将文本反转
        FileReader fr=null;
        BufferedReader br=null;
        FileWriter fw=null;
        BufferedWriter bw=null;
        try {
            fr=new FileReader("e:/abc/a.txt");
            br=new BufferedReader(fr);
            String s=null;
            StringBuffer sb=new StringBuffer();
            while((s=br.readLine())!=null){
                sb.append(s);
                sb.append("\r\n");
            }
            //反转
            sb.reverse();
            //System.out.println(sb);

            //将sb中的数据全部写入
            fw=new FileWriter("e:/abc/ccc.txt");
            bw=new BufferedWriter(fw);
            bw.write(sb.toString());
            bw.flush();

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

  • 16
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值