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

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 {

	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 ts= new TreeSet();
		
		
		
		//创建一个键盘输入的对象
		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("-------------------");
		}
		
	}
}

你可能感兴趣的:(java学习)