聽(tīng)著張孝祥老師關(guān)于緩沖區(qū)知識(shí)的課,發(fā)現(xiàn)還是有一些沒(méi)有掌握,動(dòng)手試了一下,果然發(fā)現(xiàn)了問(wèn)題。
先講一下關(guān)于java緩沖區(qū)的知識(shí),應(yīng)用程序和IO設(shè)備之間存在一個(gè)緩沖區(qū),一般流是沒(méi)有緩沖區(qū)的,但是如果存在緩沖區(qū),就會(huì)發(fā)現(xiàn)很大的問(wèn)題。
錯(cuò)誤代碼如下:為了確保問(wèn)題發(fā)生,我使用了BufferedOutputStream,使得手動(dòng)構(gòu)造出了一個(gè)緩沖區(qū)。
- 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);
-
- }
- }
|
發(fā)現(xiàn)什么問(wèn)題了嗎?
因?yàn)槿绻麤](méi)有緩沖區(qū),應(yīng)用程序每次IO都要和設(shè)備進(jìn)行通信,效率很低,因此緩沖區(qū)為了提高效率,當(dāng)寫(xiě)入設(shè)備時(shí),先寫(xiě)入緩沖區(qū),等到緩沖區(qū)有足夠多的數(shù)據(jù)時(shí),就整體寫(xiě)入設(shè)備。這就是問(wèn)題所在,上個(gè)例子中,當(dāng)我們寫(xiě)入hello時(shí),由于hello占用空間很小,所以暫時(shí)存放在緩沖區(qū)中,后來(lái)輸入流想要從文件中讀取,但是由于文件中沒(méi)有字節(jié),所以不能讀取hello。
這里,解決方法很簡(jiǎn)單,只要調(diào)用out.flush() 或者out.close()即可,這是把緩沖區(qū)的數(shù)據(jù)手動(dòng)寫(xiě)入文件。
正確代碼如下:
- 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);
-
- }
- }
|
接下來(lái)又是我遇到的一個(gè)例子,這個(gè)例子也很明顯的反應(yīng)出緩沖區(qū)的問(wèn)題。
- 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("*******是否要讀取數(shù)據(jù)?********");
- 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("********寫(xiě)入數(shù)據(jù)完畢********");
- }
-
- }
-
- 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;
- }
|
結(jié)果是沒(méi)有向文件寫(xiě)入任何數(shù)據(jù),為什么呢?
唯一的錯(cuò)誤就在main方法中沒(méi)有調(diào)用out.close(),把數(shù)據(jù)從緩沖區(qū)刷新到文件。因此用完資源即時(shí)關(guān)閉是很重要的。