java字节流的高效缓冲区及练习题

本文通过实例演示了Java中如何使用BufferedInputStream和BufferedOutputStream进行高效的文件拷贝,并给出了一道练习题,要求从键盘输入学生信息并按年龄排序后存入文件。示例代码展示了如何实现这一过程。
摘要由CSDN通过智能技术生成

----------------------java字节流的高效缓冲区及练习题--------------------------

 

高效缓冲区输入输出流构造器

BufferedInputStream(InputStream in)
          创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。

 

 

BufferedOutputStream(OutputStream out)
          创建一个新的缓冲输出流,以将数据写入指定的底层输出流。

 

 

范例:使用高效缓冲区拷贝文件

 

public  class CopyFileDemo2 {

 

    public  static  void main(String[] args) {

        //计算文件拷贝时间

        long  startTime = System.currentTimeMillis();

        copyFile();

        long  endTime = System.currentTimeMillis();

        System.out.println("文件拷贝时间:" + (endTime - startTime));

    }

   

 

    public  static void copyFile() {

        //声明高效缓冲区字节输入流

        BufferedInputStream in = null;

        //声明高效缓冲区字节输出流

        BufferedOutputStream out = null;

        try {

           //创建字节输入输出流对象

           in = new  BufferedInputStream(newFileInputStream("src/com/rl/byt/out/InputStreamDemo2.java"));

           out = new  BufferedOutputStream(new FileOutputStream("InputStreamDemo2.java"));

           byte[] bs = new  byte[1024];

           int  len = -1;

           while((len= in.read(bs)) != -1) {

               //把字节数组中的数据接入到文件中

               out.write(bs,0,len);

           }

          

        } catch (FileNotFoundException e) {

           e.printStackTrace();

        } catch (IOException e) {

           e.printStackTrace();

        }finally {

          

           try {

               if(out != null) {

                   out.close();

               }

               if(in != null) {

               in.close();

               }

           } catch (IOException e) {

               e.printStackTrace();

           }

        }

       

    }

}

 

=========================================

作业题:从键盘输入信息存储到文件中,学生按照年龄排序。

 

创建一个学生类

 

public  class Student implements Comparable<Student>{

 

    private String name ;

   

    private Integer age ;

   

    @Override

    public  int compareTo(Student o) {

        int  num = this.age - o.age;

        if(num == 0)

           num = this.name.compareTo(o.name);

          return  num;

    }

 

    public String getName() {

        return  name;

    }

 

    public  void setName(String name) {

        this.name = name;

    }

 

    public Integer getAge() {

        return  age;

    }

 

    public  void setAge(Integer age) {

        this.age = age;

    }

 

}

 

--------------------------------------------

 

创建一个输入并且向文件中写入学生信息的类

public  class ScannerTest {

 

    public  static  void main(String[] args) {

        //定义学生类的集合

        TreeSet<Student> ts = new TreeSet<Student>();

        //创建键盘输入对象

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入你要输入几个学生:");

        //获得要输入学生的数量

        int  count = sc.nextInt();

       

        for (inti = 0; i <count; i++) {

           sc = new  Scanner(System.in);

           //获得学生的姓名

           System.out.println("请输入学生的姓名:");

           String name = sc.nextLine();

           //获得学生的年龄

           System.out.println("请输入学生的年龄:");

           Integer age =sc.nextInt();

           //创建一个学生对象

           Student s = new Student();

           s.setAge(age);

           s.setName(name);

           //把学生加入到集合中

           ts.add(s);

        }

       

       

        //将学生写入文本中

           BufferedWriter bw = null;

        try {

           //创建高效缓冲区字符输出流

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

           //遍历学生类的集合

           for (Student s : ts) {

               //将学生信息写入文本并且换行

               bw.write(s.getName() + "------" + s.getAge());

               bw.newLine();

           }

           //释放缓冲区

           bw.flush();

        } catch (IOException e) {

           e.printStackTrace();

        }finally {

           //关闭高效缓冲区字符输出流

               try {

                   if(bw != null)

                   bw.close();

               } catch (IOException e) {

                   e.printStackTrace();

               }

          

        }

        System.out.println("写入文件完毕!");

       

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值