qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

Java流缓冲区问题

 听着张孝祥老师关于缓冲区知识的课,发现还是有一些没有掌握,动手试了一下,果然发现了问题。

  先讲一下关于java缓冲区的知识,应用程序和IO设备之间存在一个缓冲区,一般流是没有缓冲区的,但是如果存在缓冲区,就会发现很大的问题。

  错误代码如下:为了确保问题发生,我使用了BufferedOutputStream,使得手动构造出了一个缓冲区。

  1. import java.io.*;  
  2. public class Test {  
  3.     public static void main(String[] args) throws Exception{  
  4.         DataOutputStream out = new DataOutputStream(  
  5.                             new BufferedOutputStream(  
  6.                             new FileOutputStream("1.txt")));  
  7.         out.writeChars("hello");  
  8.         FileInputStream in = new FileInputStream("1.txt");  
  9.         int len = in.available();  
  10.         byte[] b = new byte[len];  
  11.         int actlen = in.read(b);  
  12.         String str = new String(b);  
  13.         System.out.println(str);  
  14.           
  15.     }     
  16. }

  发现什么问题了吗?

  因为如果没有缓冲区,应用程序每次IO都要和设备进行通信,效率很低,因此缓冲区为了提高效率,当写入设备时,先写入缓冲区,等到缓冲区有足够多的数据时,就整体写入设备。这就是问题所在,上个例子中,当我们写入hello时,由于hello占用空间很小,所以暂时存放在缓冲区中,后来输入流想要从文件中读取,但是由于文件中没有字节,所以不能读取hello。

  这里,解决方法很简单,只要调用out.flush() 或者out.close()即可,这是把缓冲区的数据手动写入文件。

  正确代码如下:

  1. import java.io.*;  
  2. public class Test {  
  3.     public static void main(String[] args) throws Exception{  
  4.         DataOutputStream out = new DataOutputStream(  
  5.                             new BufferedOutputStream(  
  6.                             new FileOutputStream("1.txt")));  
  7.         out.writeChars("hello");  
  8.         out.close();//inserted  
  9.         FileInputStream in = new FileInputStream("1.txt");  
  10.         int len = in.available();  
  11.         byte[] b = new byte[len];  
  12.         int actlen = in.read(b);  
  13.         String str = new String(b);  
  14.         System.out.println(str);  
  15.           
  16.     }     
  17. }

 接下来又是我遇到的一个例子,这个例子也很明显的反应出缓冲区的问题。

  1. import java.io.BufferedReader;  
  2. import java.io.FileReader;  
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5. import java.io.PrintWriter;  
  6. import java.util.Calendar;  
  7. import java.util.Date;  
  8. import java.util.GregorianCalendar;  
  9. import java.util.Scanner;  
  10. import java.util.StringTokenizer;  
  11.   
  12. public class StringTokenizerTest {  
  13.   
  14.     public static void main(String[] args) {  
  15.         Employee[] e = new Employee[3];  
  16.         e[0] = new Employee("Carl Cracker"7500019871215);  
  17.         e[1] = new Employee("Harry Hacker"500001989101);  
  18.         e[2] = new Employee("Tony Tester"400001990315);  
  19.         try {  
  20.             PrintWriter out = new PrintWriter(new FileWriter("1.txt"));  
  21.             writeData(e, out);  
  22.             // out.close();**********************************************************************   
  23.         } catch (Exception e1) {  
  24.             e1.printStackTrace();  
  25.         }  
  26.         System.out.println("*******是否要读取数据?********");  
  27.         Scanner in1 = new Scanner(System.in);  
  28.         String yes = in1.nextLine();  
  29.         if (yes.equalsIgnoreCase("YES")) {  
  30.             try {  
  31.                 BufferedReader in = new BufferedReader(new FileReader("1.txt"));  
  32.                 Employee[] result = readData(in);  
  33.                 for (int i = 0; i < result.length; i++)  
  34.                     System.out.println(result[i]);  
  35.                 in.close();  
  36.             } catch (Exception e2) {  
  37.                 e2.printStackTrace();  
  38.             }  
  39.         }  
  40.   
  41.     }  
  42.   
  43.     public static Employee[] readData(BufferedReader in) throws IOException {  
  44.         int length = Integer.parseInt(in.readLine());  
  45.         Employee[] e = new Employee[length];  
  46.         for (int i = 0; i < length; i++) {  
  47.             String line = in.readLine();  
  48.             StringTokenizer token = new StringTokenizer(line, "|");  
  49.             String name = token.nextToken();  
  50.             double salary = Double.parseDouble(token.nextToken());  
  51.             int year = Integer.parseInt(token.nextToken());  
  52.             int month = Integer.parseInt(token.nextToken());  
  53.             int day = Integer.parseInt(token.nextToken());  
  54.             e[i] = new Employee(name, salary, year, month, day);  
  55.         }  
  56.         return e;  
  57.     }  
  58.   
  59.     public static void writeData(Employee[] e, PrintWriter out) {  
  60.         out.println(e.length);  
  61.         for (int i = 0; i < e.length; i++) {  
  62.             String name = e[i].getName();  
  63.             double salary = e[i].getSalary();  
  64.             Date date = e[i].getHireDay();  
  65.             Calendar c = new GregorianCalendar();  
  66.             c.setTime(date);  
  67.             int year = c.get(Calendar.YEAR);  
  68.             int month = c.get(Calendar.MONTH) + 1;  
  69.             int day = c.get(Calendar.DAY_OF_MONTH);  
  70.             out.println(name + "|" + salary + "|" + year + "|" + month + "|"  
  71.                     + day);  
  72.   
  73.         }  
  74.         System.out.println("********写入数据完毕********");  
  75.     }  
  76.   
  77. }  
  78.   
  79. class Employee {  
  80.     public Employee(String n, double s, int year, int month, int day) {  
  81.         name = n;  
  82.         salary = s;  
  83.         GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);  
  84.         hireDay = calendar.getTime();  
  85.     }  
  86.   
  87.     public String getName() {  
  88.         return name;  
  89.     }  
  90.   
  91.     public double getSalary() {  
  92.         return salary;  
  93.     }  
  94.   
  95.     public Date getHireDay() {  
  96.         return hireDay;  
  97.     }  
  98.   
  99.     public void raiseSalary(double byPercent) {  
  100.         double raise = salary * byPercent / 100;  
  101.         salary += raise;  
  102.     }  
  103.   
  104.     public String toString() {  
  105.         return getClass().getName() + "[name=" + name + ",salary=" + salary  
  106.                 + ",hireDay=" + hireDay + "]";  
  107.     }  
  108.   
  109.     private String name;  
  110.   
  111.     private double salary;  
  112.   
  113.     private Date hireDay;  
  114. }

  结果是没有向文件写入任何数据,为什么呢?

  唯一的错误就在main方法中没有调用out.close(),把数据从缓冲区刷新到文件。因此用完资源即时关闭是很重要的。

posted on 2011-11-18 14:37 顺其自然EVO 阅读(1020) 评论(0)  编辑  收藏 所属分类: 测试学习专栏


只有注册用户登录后才能发表评论。


网站导航:
 
<2011年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜