java从入门到弃坑第十三天0A0

1.字节缓冲区流:BufferedInputStream(read() ):读取字节文件

                            BufferedOutputStream(write(byte[] b)):写入字节文件

                            该类流属于高级流,就是操作低级流的流

2.复制文件所用时间:

//A:基本字节流一次读写一个字节所用时间
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MethordDemo1 {
	public static void main(String[] args) throws IOException {
		long l=System.currentTimeMillis();
		FileOutputStream fos=new FileOutputStream("b.mp4");
		FileInputStream fis=new FileInputStream("a.mp4");
		int s;
		while((s=fis.read())!=-1){
			fos.write(s);
		}
		fis.close();
		fos.close();
		System.out.println(System.currentTimeMillis()-l);//48884
	}
}
//B:基本字节流一次读写一个字节数组
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MethordDemo1 {
	public static void main(String[] args) throws IOException {
		long l=System.currentTimeMillis();
		FileOutputStream fos=new FileOutputStream("b.mp4");
		FileInputStream fis=new FileInputStream("a.mp4");
		int s;
		byte a[]=new byte[1024];
		while((s=fis.read(a))!=-1){
			fos.write(a, 0, s);
		}
		fis.close();
		fos.close();
		System.out.println(System.currentTimeMillis()-l);//108

	}
}
//C:高效字节流一次读写一个字节
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MethordDemo2 {
	public static void main(String[] args) throws IOException {
		long l=System.currentTimeMillis();
		BufferedInputStream bis=new BufferedInputStream(new FileInputStream("a.mp4"));
		BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("c.mp4"));
		int s;
		while((s=bis.read())!=-1){
			bos.write(s);
		}
		bis.close();
		bos.close();
		System.out.println(System.currentTimeMillis()-l);//278
	}
}
//D:高效字节流一次读写一个字节数组
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class MethordDemo2 {
	public static void main(String[] args) throws IOException {
		long l=System.currentTimeMillis();
		BufferedInputStream bis=new BufferedInputStream(new FileInputStream("a.mp4"));
		BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("c.mp4"));
		int s;
		byte chs[]=new byte[1024];
		while((s=bis.read(chs))!=-1){
			bos.write(chs, 0, s);
		}
		bis.close();
		bos.close();
		System.out.println(System.currentTimeMillis()-l);//36

	}
}

一般使用字节流一次读取一个字节数组。

3.编码问题:
          编码:把我们能看懂的字符转换为看不懂的数据。

          解码:把我们看不懂的数据转换为字符。

          public byte[] getBytes(String charsetName)  按照给定的编码方式,编码字节数组(gbk或者utf-8)

          String(byte[] bytes, String charsetName) 按照给定的编码方式解码字符数组

          gbk用两个字节表示一个中文字符,而uft-8用三个字节表示一个中文字符,所以用什么编码,就用什么解码。

4.字符流:OutputStreamWriter/InputStreamReader:字符流的构造方法中只包含字节流对象,因此要创建字符流

                 象,就要通过该构造方法,将字节流对象转化成字符流对象(过程中注意编码问题,一般情况默认)。

        exp:OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"));

                  InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"));
    将上述过程简化,用其子类FileReader/FileWriter莱创建对象。

5.flush()和close()的区别:flush :刷新缓冲区,流对象可以继续

                                          close: 先刷新缓冲区,再关闭流对象。流对象不可以继续使用了

   在字符流的使用过程中,程序和字节流大同小异,在读取或写入时只注意将处理的对象从字节变成字符。而且在使用中,最好用字节流处理文件,因为一个字符是两个字节,如果文件大小为奇数个字节,用字符流就会导致文件损坏

6.字符高效流:BufferedReader:字符缓冲输入流

                                   构造方法:BufferedReader(Reader in)
                                                     成员方法:public String readLine():读取该行字符串,到达末尾返回null。

                          BufferedWriter:字符缓冲输出流

                                   构造方法:BufferedWriter(Writer out)

                                   成员方法:public void newLine():会根据系统来确定写入不同的换行符

exp:键盘录入5个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从低到高存入文本文件

public class Student {
	private String name;
	private int yuwen;
	private int math;
	private int english;
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Student(String name, int yuwen, int math, int english) {
		super();
		this.name = name;
		this.yuwen = yuwen;
		this.math = math;
		this.english = english;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getYuwen() {
		return yuwen;
	}
	public void setYuwen(int yuwen) {
		this.yuwen = yuwen;
	}
	public int getMath() {
		return math;
	}
	public void setMath(int math) {
		this.math = math;
	}
	public int getEnglish() {
		return english;
	}
	public void setEnglish(int english) {
		this.english = english;
	}
	@Override
	public String toString() {
		return "[name=" + name + ", yuwen=" + yuwen + ", math=" + math
				+ ", english=" + english + "]";
	}
}

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;
public class ScoreTest {
	public static void main(String[] args) throws IOException {
		Scanner sc1=new Scanner(System.in);
		Scanner sc2=new Scanner(System.in);
		TreeSet<Student> t=new TreeSet<>(new Comparator<Student>() {
			public int compare(Student o1, Student o2) {
				int num=o1.getEnglish()+o1.getMath()+o1.getYuwen()-o2.getEnglish()-o2.getMath()-o2.getYuwen();
				int num2=num==0?o1.getName().compareTo(o2.getName()):num;
				return num2;
			}
		});
		for (int i = 0; i < 5; i++) {
			Student s=new Student();
			s.setName(sc1.nextLine());
			s.setMath(sc2.nextInt());
			s.setYuwen(sc2.nextInt());
			s.setEnglish(sc2.nextInt());
			t.add(s);
		}
		BufferedWriter bw=new BufferedWriter(new FileWriter("e.txt"));
		
		for (Student u : t) {
			bw.write(u.toString());
			bw.newLine();
			bw.flush();
			System.out.println(u);
		}
		bw.close();
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值