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", 75000, 1987, 12, 15);  
  17.         e[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);  
  18.         e[2] = new Employee("Tony Tester", 40000, 1990, 3, 15);  
  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(),把数据从缓冲区刷新到文件。因此用完资源即时关闭是很重要的。

本文出自seven的测试人生公众号最新内容请见作者的GitHub页:http://qaseven.github.io/

时间: 2024-09-14 12:16:50

Java流缓冲区问题的相关文章

Java中IO流缓冲区的装饰模式的体现

一.Java中IO流缓冲区 import java.io.*; public class BufferedTest {public static void copy1(){InputStream is = null;OutputStream os = null;try{is = new FileInputStream("c:\\xy1.jpg");os = new FileOutputStream("d:\\xy2.jpg");int len = 0;byte[]

filereader-关于java 流的初级问题

问题描述 关于java 流的初级问题 FileReader fr = new FileReader(""D:\c语言感悟.txt""); int ch; while (-1 !=(ch=fr.read())) //20行 { System.out.printf(""%c"" (char)ch); } 请问我将代码改成下面那样为什么输出就会出错? FileReader fr = new FileReader("&quo

java NIO 缓冲区满了怎么办?

问题描述 java NIO 缓冲区满了怎么办? 客户端一次性向服务器传输大量数据时:如(缓冲区只有50:但是要写入的数据却为几万),虽然数据会被分成大小不一的包,有的小于缓冲区,有的却会大于缓冲区,导致超出缓冲区的数据部分丢失,应该怎么办呢?

JAVA流控及超流控后的延迟处理实例_java

本文实例讲述了JAVA流控及超流控后的延迟处理方法.分享给大家供大家参考.具体实现方法如下: 流控检查(每半秒累计,因此最小留空阀值只能做到每秒2条): 复制代码 代码如下: import java.text.SimpleDateFormat; import java.util.Date; import java.lang.Thread;   /**  * 流量控制  *  * @author chenx  */ public class OverflowController {       p

java流字符乱码问题,求助!

问题描述 java流字符乱码问题,求助! 地址https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=15850781443,结果为GBK编码,获取手机号码归属地以后,发现获得字符结果中中文乱码,麻烦大神指教,谢谢!以下提出部分代码. HttpClient client = new DefaultHttpClient();//浏览器客户端 HttpGet request = new HttpGet(sb.toString());//ge

java 流式布局案例,跑不通

问题描述 java 流式布局案例,跑不通 /* 网格布局演示 / package class8; import java.awt.; import javax.swing.*; public class Demo_4 extends JFrame{ //定义组件 int size=9; JButton jbs[]=new JButton[size]; public static void main(String[] args) { // TODO Auto-generated method st

Core Java - 流(Stream) - 字节流和字符流(一)

0. 概述: Java中基于流的I/O构建在4个抽象类之上, 其中2个是字节流,另外2个是字符流. 字节流: InputStream / OutputStream 当操作字节或其它二进制对象时,应当使用字节流.   字符流: Reader / Writer 当操作字符或字符串时,应当使用字符流.   1. InputStream InputStream:   输入字节流,它是一个抽象类. 常用主要方法: int read() 返回代表下一个可用字节的整数,当文件达到末尾时,返回-1.   int

Java 视图缓冲区 是什么意思?? 新手请指点

问题描述 finalintcount=50;byteBufferbuf=ByteBuffer.allocate(count*8);//请问这里为什么要乘以8LongBufferlongBuffer=buf.asLongBuffer();//asLongBuffer是视图缓冲区,但是这个视图缓冲区是什么意思?我在Google查不到什么详细的介绍. 解决方案

java socket缓冲区大小问题

问题描述 我在用Socket接收数据的过程中遇到一个问题在局域网中服务器端传过来的数据特别快,数据量也比较大(音频视频数据),客户端处理不过来,造成了Read函数返回-1.我猜测可能和底层的缓冲区有关系,如果调用setReceiveBufferSize函数增大缓冲区就会好一点,但是时间长了,缓冲区满又会出现相同的问题.请问是什么原因,如何解决呢? 解决方案 解决方案二:没人吗?解决方案三:不太清楚帮顶解决方案四:原本我也用socket写了收发文件还用了分包拆包,加包头信息等网络传送的东西.最后我