字节流的高效缓冲区基础学习笔记

本文介绍了一种使用高效缓冲区进行文件复制的方法,并对比了其与普通字节复制的效率。此外,还展示了如何通过键盘输入收集学生信息,并将这些信息存储到文件中,同时确保学生按年龄排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、列1:比较高效缓冲区和普通字节的运行时长

package com.rl.byt.output;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * 使用高效缓冲区来复制文件来比较时长
 * @author Administrator
 *
 */
public class BufferedCopyFileDemo {
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		copyFile2();
		long endTime = System.currentTimeMillis();
		System.out.println("耗时"+(startTime - endTime));
		
				
	}
	/**
	 * 单个字节的复制
	 */
	public static void copyFile(){
		InputStream in = null;
		OutputStream out = null;
		try {
			//创建文件的输入流对象
			in = new FileInputStream("E:/eclipse/eclipse/workspace/io_demo1/src/com/rl/byt/output/OutputStreamDemo.java");
			//创建文件的输出流对象
			out = new FileOutputStream("OutputStreamDemo.java");
			byte[] bs = new byte[1024];
			//定义返回读取的长度
			int len = -1;
			while((len = in.read()) != -1){
				//把字节数组中的数据写入到文件中
				out.write(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 static void copyFile2(){
		InputStream in = null;
		OutputStream out = null;
		try {
			//创建文件的高效缓冲区输入流对象
			in = new BufferedInputStream(new FileInputStream("E:/eclipse/eclipse/workspace/io_demo1/src/com/rl/byt/output/OutputStreamDemo.java"));
			//创建文件的高效缓冲区输出流对象
			out = new BufferedOutputStream(new FileOutputStream("OutputStreamDemo.java"));
			byte[] bs = new byte[1024];
			//定义返回读取的长度
			int len = -1;
			while((len = in.read()) != -1){
				//把字节数组中的数据写入到文件中
				out.write(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();
			}
		}
		
		
	}
}

2、列2:从键盘输入学生信息存储到文件中,学生按着年龄排列

package com.rl.byt.model;

public class Student implements Comparable<Student> {

	private String name;
	private Integer age;
	
	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;
	}
	


	//接口后错误中加的方法
	@Override
	public int compareTo(Student o) {
		int num = this.age - o.age;
		if(num==0){
			num = this.name.compareTo(o.getName());
		}
		return 0;
	}

}

package com.rl.byt.model;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;
/**
 * 从键盘输入学生信息存储到文件中,学生按着年龄排列
 * @author Administrator
 *
 */
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 cont = sc.nextInt();
		for(int i = 0;i < cont;i++){
			sc = new Scanner(System.in);
			System.out.println("请输入学生的姓名:");
			//获得学生的姓名
			String name = sc.nextLine();
			System.out.println("请输入学生的年龄:");
			//获得学生的年龄
			Integer age = sc.nextInt();
			//创建学生对象
			Student stu = new Student();
			stu.setAge(age);
			stu.setName(name);
			//把学生加入到集合中
			ts.add(stu);
			
		}
		//定义高效缓冲区的字符输出流
		BufferedWriter bw =null;
		try {
			bw = new BufferedWriter(new FileWriter("student.txt"));
			for(Student stu:ts){
				bw.write(stu.getName()+"------"+stu.getAge());
				bw.newLine();
			}
			bw.flush();
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(bw != null){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			System.out.println("写入文件完毕");
			System.out.println("-------------------");
		}
		
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值