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[] buffer = new byte[1024];
  while ((len = is.read(buffer)) != -1)
  {
    os.write(buffer, 0, len);
  }
  }
  catch (IOException ex)
  {
  throw new RuntimeException("文件操作出错");
  }
  finally
  {
  try
  {
    if (null != is)
    {
    is.close();
    }
  }
  catch (IOException ex)
  {
    throw new RuntimeException("输入流关闭出错");
  }

  try
  {
    if (null != os)
    {
    os.close();
    }
  }
  catch (IOException ex)
  {
    throw new RuntimeException("输出流关闭出错");
  }
  }
 }

 public static void copy2()
 {
  InputStream is = null;
  BufferedInputStream bis = null;
  OutputStream os = null;
  BufferedOutputStream bos = null;
  try
  {
  is = new FileInputStream("c:\\xy1.jpg");
  bis = new BufferedInputStream(is);
  os = new FileOutputStream("d:\\xy2.jpg");
  bos = new BufferedOutputStream(os);
  int len = 0;
  byte[] buffer = new byte[1024];
  while ((len = bis.read(buffer)) != -1)
  {
    bos.write(buffer, 0, len);
  }
  }
  catch (IOException ex)
  {
  throw new RuntimeException("文件操作出错");
  }
  finally
  {
  try
  {
    if (null != bis)
    {
    bis.close();
    }
  }
  catch (IOException ex)
  {
    throw new RuntimeException("输入流关闭出错");
  }

  try
  {
    if (null != bos)
    {
    bos.close();
    }
  }
  catch (IOException ex)
  {
    throw new RuntimeException("输出流关闭出错");
  }
  }
 }
}
copy1方法是普通的利用字节流读写文件的例子。JDK提供了流的缓冲区的概念用来提高读写效率,在copy2中可以提现。利用了设计模式的装饰模式的概念。

Java 8 彻底改变数据库访问


二、利用装饰模式自定义读取行方法

字符流的缓冲区BufferedReader提供了一个读取行的方法,可以一次读取文本文件的一行。利用装饰模式自己实现一个读取行的方法。
public class MyReadLine
{
 private Reader reader;

 public MyReadLine(Reader reader)
 {
  super();
  this.reader = reader;
 }

 public String myReadLine() throws IOException
 {
  // 字符缓冲区
  StringBuilder sbuilder = new StringBuilder();
  // 单个字符的ASCII码值
  int len = 0;
  // 未读到文件的末尾
  while ((len = reader.read()) != -1)
  {
  // windows中换行符是\r\n
  if (len == '\r') continue;
  if (len == '\n')
  {
    // 读取完了一行,返回该行内容
    return sbuilder.toString();
  }
  else
  {
    // 将读到的有效字符存入缓冲区
    sbuilder.append((char) len);
  }
  }
  // 最后一行可能没有换行符,若判断出现换行符再返回值就读取不到最后一行。所以判断缓冲区内是否有值,若有值就返回。
  if (sbuilder.length() != 0)
  {
  return sbuilder.toString();
  }
  return null;
 }

 public void myclose() throws IOException
 {
  reader.close();
 }

 public Reader getReader()
 {
  return reader;
 }

 public void setReader(Reader reader)
 {
  this.reader = reader;
 }
}

public class MyReadLineTest
{
 public static void main(String[] args)
 {
  MyReadLine myReader = null;
  try
  {
  StringBuilder sbresult = new StringBuilder();
  myReader = new MyReadLine(new FileReader("c:\\aaa.txt"));
  String line = null;
  while ((line = myReader.myReadLine()) != null)
  {
    sbresult.append(line);
  }
  System.out.println(sbresult.toString());
  }
  catch (IOException e)
  {
  throw new RuntimeException("读取文件异常");
  }
  finally
  {
  try
  {
    if (null != myReader)
    {
    myReader.myclose();
    }
  }
  catch (IOException e)
  {
    throw new RuntimeException("关闭失败");
  }
  }
 }
}

相关内容