Java学习DAY13

第十二章 异常02

1.字节缓冲区流

在这里插入图片描述

public class cest{
    public static void main(String[] args) throws IOException {
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("b.txt"));
        bos.write("hello".getBytes());
        bos.close();

        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("b.txt"));

        
        int by;
        while((by=bis.read())!=-1){
            System.out.print((char)by);//读一个字节
        }

         

        byte[] hyt=new byte[1024];
        int len;
        while((len=bis.read(hyt))!=-1){
            System.out.print(new String(hyt,0,len));//读一个字节数组
        }

        bis.close();            
    }
}

计算在4种方式下,复制一个图片所需时间
在这里插入图片描述

public class cest{
    public static void main(String[] args) throws IOException {
        long start= System.currentTimeMillis();
        method1();
        //method2();
        //method3();
        //method4();
        
        long end=System.currentTimeMillis();
        
    }
    private static void method1() throws IOException {
        FileInputStream fi=new FileInputStream("D:\\图片.avi");
        FileOutputStream fl=new FileOutputStream("复制图片.avi");
        int by;
        while((by=fi.read())!=-1){
            fl.write(by);
        }
        fi.close();
        fl.close();

    }
    private static void method2() throws IOException {
        FileInputStream fi=new FileInputStream("D:\\图片.avi");
        FileOutputStream fl=new FileOutputStream("复制图片.avi");

        byte[] bt=new byte[1024];
        int len;
        while((len=fi.read(bt))!=-1){
            fl.write(bt,0,len);
        }
        fi.close();
        fl.close();
    }

    private static void method3() throws IOException {
        BufferedInputStream fi=new BufferedInputStream(new FileInputStream("D:\\图片.avi"));
       BufferedOutputStream fl=new BufferedOutputStream(new FilterOutputStream("复制图片.avi"));
        int by;
        while((by=fi.read())!=-1){
            fl.write(by);
        }
        fi.close();
        fl.close();

    }

    private static void method4() throws IOException {
        BufferedInputStream fi=new BufferedInputStream(new FileInputStream("D:\\图片.avi"));
        BufferedOutputStream fl=new BufferedOutputStream(new FilterOutputStream("复制图片.avi"));
        byte[] bt=new byte[1024];
        int len;
        while((len=fi.read(bt))!=-1){
            fl.write(bt,0,len);
        }
        fi.close();
        fl.close();

    }
}
2.转换流

在这里插入图片描述
在这里插入图片描述

3.string类的编码解码

在这里插入图片描述

      String s= "你好";
      byte[] bys= s.getBytes("GBK");//[-60, -29, -70, -61]
      //byte[] bys= s.getBytes(StandardCharsets.UTF_8);//[-28, -67, -96, -27, -91, -67]
      System.out.println(Arrays.toString(bys));

     String ss= new String(bys,"GBK");
     //String ss=new String(bys,"UTF_8");
     System.out.println(ss);
4.转换流中的编码和解码

在这里插入图片描述

     OutputStreamWriter  osw=new OutputStreamWriter(new FileOutputStream("b.txt"),"UTF-8") ;
     osw.write("你好");
     osw.close();

     InputStreamReader ins=new InputStreamReader(new FileInputStream("b.txt"),"UTF-8");
     int ch;
     while((ch=ins.read())!=-1){
         System.out.print((char)ch);
     }
     ins.close();
OutputSteamWriter写数据的五种方式

在这里插入图片描述

public class cest{
    public static void main(String[] args) throws IOException {
     OutputStreamWriter  osw=new OutputStreamWriter(new FileOutputStream("b.txt")) ;
     osw.write(97);
     osw.write('a');

     char[]  chs={'a','b','c','d','e'};
     osw.write(chs);

     osw.write(chs,1,3);

     osw.write("hello");
     
     osw.write("hello",0,3);
     
     osw.flush();
     osw.close();

    }
}
InputSteamWriter读数据的两种方式

在这里插入图片描述

public class cest{
    public static void main(String[] args) throws IOException {
     InputStreamReader osw=new InputStreamReader(new FileInputStream("b.txt")) ;
     int by;
     while((by=osw.read())!=-1){
            System.out.print((char)by);
     }
     

     char[] bt=new char[1024];
     int len;
     while((len=osw.read(bt))!=-1){
         System.out.print(new String(bt,0,len));
     }

     osw.close();


    }
}

练习1:复制java文件

public class cest{
    public static void main(String[] args) throws IOException {
     FileReader isr=new FileReader("D:/java/myproject/src/heima/Human1.java") ;
     FileWriter osw = new FileWriter("c.java");
     int by;
     while ((by = isr.read()) != -1) {
                osw.write(by);
     }

     /*
     char[] bt = new char[1024];
     int len;
     while ((len = isr.read(bt)) != -1) {
                osw.write(bt, 0, len);
     }

      */

     osw.close();
     isr.close();


    }
}
5.字符缓冲区流

在这里插入图片描述

public class cest{
    public static void main(String[] args) throws IOException {
        //BufferedWriter bw=new BufferedWriter(new FileWriter("b.txt"));
        //bw.write("hello");
        //bw.close();

        BufferedReader br=new BufferedReader(new FileReader("D:/java/myproject/src/heima/Human1.java"));
        int ch;
        while((ch=br.read())!=-1){
            System.out.print((char)ch);
        }
        br.close();

    }
}

案例1:复制文件

public static void main(String[] args) throws IOException {
        BufferedWriter bw=new BufferedWriter(new FileWriter("a.txt"));
        BufferedReader br=new BufferedReader(new FileReader("b.txt"));
        int ch;
        while((ch=br.read())!=-1){
            bw.write(ch);
        }
        br.close();
        bw.close();

    }

在这里插入图片描述

public static void main(String[] args) throws IOException {
        /*
        BufferedWriter bw=new BufferedWriter(new FileWriter("b.txt"));
        for(int i=0;i<3;i++){
            bw.write("hello");
            bw.newLine();
            bw.flush();
        }
        bw.close();
        */
        BufferedReader br=new BufferedReader(new FileReader("b.txt"));

        String line ;
        while((line=br.readLine())!=null){
            System.out.println(line);

        }
        br.close();
    }

案例2:复制java文件

public static void main(String[] args) throws IOException {

        BufferedWriter bw=new BufferedWriter(new FileWriter("c.java"));
        BufferedReader br=new BufferedReader(new FileReader("D:/java/myproject/src/heima/Human1.java"));

        String line ;
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();

        }
        br.close();
        bw.close();
    }

案例3:5种方式复制文本文件
在这里插入图片描述

public class cest{
    public static void main(String[] args) throws IOException {
        //method1();
        //method2();
        //method3();
        //method4();
        method5();
    }


    private static void method1() throws IOException {
        FileReader fr=new FileReader("b.txt");
        FileWriter fw=new FileWriter("a.txt");
        int ch;
        while((ch=fr.read())!=-1){
            fw.write(ch);
        }
        fr.close();
        fw.close();
    }
    private static void method2() throws IOException {
        FileReader fr=new FileReader("b.txt");
        FileWriter fw = new FileWriter("a.txt");

        char[]  chs=new char[1024];
        int len;
        while((len=fr.read(chs))!=-1){
            fw.write(new String(chs,0,len));
        }
        fr.close();
        fw.close();

    }
    private static void method3() throws IOException{
        BufferedReader br=new BufferedReader(new FileReader("b.txt"));
        BufferedWriter bw=new BufferedWriter(new FileWriter("a.txt"));
        int ch;
        while((ch=br.read())!=-1){
            bw.write(ch);
        }
        br.close();
        bw.close();
    }

    private static void method4() throws IOException{
        BufferedReader br=new BufferedReader(new FileReader("b.txt"));
        BufferedWriter bw=new BufferedWriter(new FileWriter("a.txt"));

        char[]  chs=new char[1024];
        int len;
        while((len=br.read(chs))!=-1){
            bw.write(new String(chs,0,len));
        }
        br.close();
        bw.close();
    }
    private static void method5() throws IOException{
        BufferedReader br=new BufferedReader(new FileReader("b.txt"));
        BufferedWriter bw=new BufferedWriter(new FileWriter("a.txt"));

        String line;
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        br.close();
        bw.close();
    }


}

案例4:把集合中的字符串复制到文本文件

public static void main(String[] args) throws IOException {
        ArrayList<String> array=new ArrayList<String>();
        array.add("helloworld");
        array.add("java");

        BufferedWriter bw=new BufferedWriter(new FileWriter("b.txt"));
        for(String s:array){
            bw.write(s);
            bw.newLine();
            bw.flush();
        }
        bw.close();

    }

案例5:从文本文件读取数据到集合,并遍历集合

public static void main(String[] args) throws IOException {
        ArrayList<String> array=new ArrayList<String>();
        BufferedReader bw=new BufferedReader(new FileReader("b.txt"));
        String line;
        while((line=bw.readLine())!=null){
            array.add(line);
        }
        bw.close();

        for(String s:array){
            System.out.println(s);
        }

    }

案例6:把集合中的学生数据存储到文本文件,每一个学生数据作为一行数据

public static void main(String[] args) throws IOException {
        ArrayList<Student> array=new ArrayList<Student>();
        Student s1=new Student("Lily",18);
        Student s2 =new Student("Bob",19);
        Student s3=new Student("Jack",20);
        array.add(s1);
        array.add(s2);
        array.add(s3);

        BufferedWriter bw=new BufferedWriter(new FileWriter("b.txt"));

        for(Student s:array){
            StringBuilder sb=new StringBuilder();
            sb.append(s.getName()).append(",").append(s.getAge());
            bw.write(sb.toString());
            bw.newLine();
            bw.flush();

        }

        bw.close();
    }

案例7:把文本文件中的学生数据存储到集合,每一个学生数据作为一行数据,遍历集合

public static void main(String[] args) throws IOException {
        ArrayList<Student> array=new ArrayList<Student>();
        BufferedReader bw=new BufferedReader(new FileReader("b.txt"));

        String line;
        while((line=bw.readLine())!=null){
            String[] strArray=line.split(",");
            Student s=new Student();
            s.setName(strArray[0]);
            s.setAge(Integer.parseInt(strArray[1]));

            array.add(s);
        }
        bw.close();
        for(Student s:array){
            System.out.println(s.getName()+"-----"+s.getAge());

        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值