听着张孝祥老师关于缓冲区知识的课,发现还是有一些没有掌握,动手试了一下,果然发现了问题。
先讲一下关于java缓冲区的知识,应用程序和IO设备之间存在一个缓冲区,一般流是没有缓冲区的,但是如果存在缓冲区,就会发现很大的问题。
错误代码如下:为了确保问题发生,我使用了BufferedOutputStream,使得手动构造出了一个缓冲区。
- import java.io.*;
- public class Test {
- public static void main(String[] args) throws Exception{
- DataOutputStream out = new DataOutputStream(
- new BufferedOutputStream(
- new FileOutputStream("1.txt")));
- out.writeChars("hello");
- FileInputStream in = new FileInputStream("1.txt");
- int len = in.available();
- byte[] b = new byte[len];
- int actlen = in.read(b);
- String str = new String(b);
- System.out.println(str);
-
- }
- }
|
发现什么问题了吗?
因为如果没有缓冲区,应用程序每次IO都要和设备进行通信,效率很低,因此缓冲区为了提高效率,当写入设备时,先写入缓冲区,等到缓冲区有足够多的数据时,就整体写入设备。这就是问题所在,上个例子中,当我们写入hello时,由于hello占用空间很小,所以暂时存放在缓冲区中,后来输入流想要从文件中读取,但是由于文件中没有字节,所以不能读取hello。
这里,解决方法很简单,只要调用out.flush() 或者out.close()即可,这是把缓冲区的数据手动写入文件。
正确代码如下:
- import java.io.*;
- public class Test {
- public static void main(String[] args) throws Exception{
- DataOutputStream out = new DataOutputStream(
- new BufferedOutputStream(
- new FileOutputStream("1.txt")));
- out.writeChars("hello");
- out.close();//inserted
- FileInputStream in = new FileInputStream("1.txt");
- int len = in.available();
- byte[] b = new byte[len];
- int actlen = in.read(b);
- String str = new String(b);
- System.out.println(str);
-
- }
- }
|
接下来又是我遇到的一个例子,这个例子也很明显的反应出缓冲区的问题。
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.GregorianCalendar;
- import java.util.Scanner;
- import java.util.StringTokenizer;
-
- public class StringTokenizerTest {
-
- public static void main(String[] args) {
- Employee[] e = new Employee[3];
- e[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15);
- e[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
- e[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);
- try {
- PrintWriter out = new PrintWriter(new FileWriter("1.txt"));
- writeData(e, out);
- // out.close();**********************************************************************
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- System.out.println("*******是否要读取数据?********");
- Scanner in1 = new Scanner(System.in);
- String yes = in1.nextLine();
- if (yes.equalsIgnoreCase("YES")) {
- try {
- BufferedReader in = new BufferedReader(new FileReader("1.txt"));
- Employee[] result = readData(in);
- for (int i = 0; i < result.length; i++)
- System.out.println(result[i]);
- in.close();
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
-
- }
-
- public static Employee[] readData(BufferedReader in) throws IOException {
- int length = Integer.parseInt(in.readLine());
- Employee[] e = new Employee[length];
- for (int i = 0; i < length; i++) {
- String line = in.readLine();
- StringTokenizer token = new StringTokenizer(line, "|");
- String name = token.nextToken();
- double salary = Double.parseDouble(token.nextToken());
- int year = Integer.parseInt(token.nextToken());
- int month = Integer.parseInt(token.nextToken());
- int day = Integer.parseInt(token.nextToken());
- e[i] = new Employee(name, salary, year, month, day);
- }
- return e;
- }
-
- public static void writeData(Employee[] e, PrintWriter out) {
- out.println(e.length);
- for (int i = 0; i < e.length; i++) {
- String name = e[i].getName();
- double salary = e[i].getSalary();
- Date date = e[i].getHireDay();
- Calendar c = new GregorianCalendar();
- c.setTime(date);
- int year = c.get(Calendar.YEAR);
- int month = c.get(Calendar.MONTH) + 1;
- int day = c.get(Calendar.DAY_OF_MONTH);
- out.println(name + "|" + salary + "|" + year + "|" + month + "|"
- + day);
-
- }
- System.out.println("********写入数据完毕********");
- }
-
- }
-
- class Employee {
- public Employee(String n, double s, int year, int month, int day) {
- name = n;
- salary = s;
- GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
- hireDay = calendar.getTime();
- }
-
- public String getName() {
- return name;
- }
-
- public double getSalary() {
- return salary;
- }
-
- public Date getHireDay() {
- return hireDay;
- }
-
- public void raiseSalary(double byPercent) {
- double raise = salary * byPercent / 100;
- salary += raise;
- }
-
- public String toString() {
- return getClass().getName() + "[name=" + name + ",salary=" + salary
- + ",hireDay=" + hireDay + "]";
- }
-
- private String name;
-
- private double salary;
-
- private Date hireDay;
- }
|
结果是没有向文件写入任何数据,为什么呢?
唯一的错误就在main方法中没有调用out.close(),把数据从缓冲区刷新到文件。因此用完资源即时关闭是很重要的。
本文出自seven的测试人生公众号最新内容请见作者的GitHub页:http://qaseven.github.io/