进入旧版 | 服务项目 | 成功案例 | 联系方式 | 过客留言 | 友情链接
   
设为首页
加入收藏
联系我们
网站首页 | 新闻资讯 | 操作系统 | 办公软件 | 网络软件 | 工具软件 | 媒体动画 | 网页制作 | 网站开发 | 程序开发 | 平面设计
Photoshop视频教程 | Word入门 | Flash入门 | JScript | VBScript | ASP | PHP | ADO | 网页特效 | 3DS MAX6.0命令 | 系统进程
您当前的位置:GOODSGY电脑学习网 -> 网站开发 -> JSP -> 文章内容  
调整JavaTM 的I/O性能(三)(zt)

压缩


Java提供了对字节流进行压缩和解压缩的类。它们可以在java.util.zip包中被找到,同时也作为Jar文件的基 础(Jar文件是具有一个清单的Zip文件)。


以下的程序采用一个单一的输入文件,并且生成一个压缩了的Zip输出文件,该程序带有一个表示输入文件的 入口项。


      import java.io.*;

      import java.util.zip.*;



      public class compress {

          public static void doit(String filein, String fileout) {

              FileInputStream fis = null;

              FileOutputStream fos = null;

              try {

                  fis = new FileInputStream(filein);

                  fos = new FileOutputStream(fileout);

                  ZipOutputStream zos = new ZipOutputStream(fos);

                  ZipEntry ze = new ZipEntry(filein);

                  zos.putNextEntry(ze);

                  final int BUFSIZ = 4096;

                  byte inbuf[] = new byte[BUFSIZ];

                  int n;

                  while ((n = fis.read(inbuf)) != -1)

                      zos.write(inbuf, 0, n);

                  fis.close();

                  fis = null;

                  zos.close();

                  fos = null;

              }

              catch (IOException e) {

                  System.err.println(e);

              }

              finally {

                  try {

                      if (fis != null)

                          fis.close();

                      if (fos != null)

                          fos.close();

                  }

                  catch (IOException e) {

                  }

              }

          }



          public static void main(String args[]) {

              if (args.length != 2) {

                  System.err.println("missing filenames");

                  System.exit(1);

              }

              if (args[0].equals(args[1])) {

                  System.err.println("filenames are identical");

                  System.exit(1);

              }

              doit(args[0], args[1]);

          }

      }

下一个程序正好相反,采用假定只有一个入口项的Zip输入文件,并且将该项解压到指定的输出文件:


      import java.io.*;

      import java.util.zip.*;



      public class uncompress {

          public static void doit(String filein, String fileout) {

              FileInputStream fis = null;

              FileOutputStream fos = null;

              try {

                  fis = new FileInputStream(filein);

                  fos = new FileOutputStream(fileout);

                  ZipInputStream zis = new ZipInputStream(fis);

                  ZipEntry ze = zis.getNextEntry();

                  final int BUFSIZ = 4096;

                  byte inbuf[] = new byte[BUFSIZ];

                  int n;

                  while ((n = zis.read(inbuf, 0, BUFSIZ)) != -1)

                      fos.write(inbuf, 0, n);

                  zis.close();

                  fis = null;

                  fos.close();

                  fos = null;

              }

              catch (IOException e) {

                  System.err.println(e);

              }

              finally {

                  try {

                      if (fis != null)

                          fis.close();

                      if (fos != null)

                          fos.close();

                  }

                  catch (IOException e) {

                  }

              }

          }



          public static void main(String args[]) {

              if (args.length != 2) {

                  System.err.println("missing filenames");

                  System.exit(1);

              }

              if (args[0].equals(args[1])) {

                  System.err.println("filenames are identical");

                  System.exit(1);

              }

              doit(args[0], args[1]);

          }

      }

压缩是有助还是损害I/O性能很大程度上依赖于本地硬件设置;尤其是处理器和磁盘驱动器的相对速度。采用 Zip技术进行压缩,典型地将数据量减少50%,但是有一些压缩和解压的时间开销。在带IDE磁盘驱动器的 300-MHz Pentium PC上,对一个大的压缩文本文件(5-10MB)的实验表明,从磁盘读取压缩文件比非压缩文件 快将近1/3。


充分展现压缩优越性的一个例子是在写入象软盘这样的低速介质。使用高速处理器 (300 MHz Pentium)和低 速软盘(PC上常用的软盘驱动器)进行的测试显示,压缩一个大文本文件,然后写入软盘,比直接将文件拷 贝到软盘快50%。


高速缓存


关于硬件的高速缓存的详细讨论超出了本文的范围。但是,有时软件高速缓存 能够加速I/O。考虑这样一个 例子,需要以随机的方式读取文本文件的某些行。完成的方法之一是读入所有的行,并且把它们存储在 ArrayList(一个与Vector相似的collection类)中:


      import java.io.*;

      import java.util.ArrayList;



      public class LineCache {

          private ArrayList list = new ArrayList();



          public LineCache(String fn) throws IOException {

              FileReader fr = new FileReader(fn);

              BufferedReader br = new BufferedReader(fr);

              String ln;

              while ((ln = br.readLine()) != null)

                  list.add(ln);

              br.close();

          }



          public String getLine(int n) {

              if (n < 0)

                  throw new IllegalArgumentException();



              return (n < list.size() ?

                  (String)list.get(n) : null);

          }



          public static void main(String args[]) {

              if (args.length != 1) {

                  System.err.println("missing filename");

                  System.exit(1);

              }

              try {

                  LineCache lc = new LineCache(args[0]);

                  int i = 0;

                  String ln;

                  while ((ln = lc.getLine(i++)) != null)

                      System.out.println(ln);

              }

              catch (IOException e) {

                  System.err.println(e);

              }

          }

      }

getLine方法被用来检索任意的一行。这项技术非常有用,但对于大文件而言,显然需要使用了大量的内存, 因此具有局限性。一个替代方法是仅仅记住最近被请求的100行,必要时从磁盘上读取其他行。在存在行的存 储局部性这种情况下,这个方案运行良好,但是需要真正的随机存储时就没有这么好了。


标志化(Tokenization)


标志化(Tokenization)指将字节或者字符序列拆散成象词一样的逻辑块的过程。Java提供了StreamTokenizer 类,可以进行如下的操作:


      import java.io.*;



      public class token1 {

          public static void main(String args[]) {

              if (args.length != 1) {

                  System.err.println("missing filename");

                  System.exit(1);

              }

              try {

                  FileReader fr = new FileReader(args[0]);

                  BufferedReader br = new BufferedReader(fr);

                  StreamTokenizer st = new StreamTokenizer(br);

                  st.resetSyntax();

                  st.wordChars('a', 'z');

                  int tok;

                  while ((tok = st.nextToken()) !=

                              StreamTokenizer.TT_EOF) {

                      if (tok == StreamTokenizer.TT_word)

                          ; // st.sval has token

                  }

                  br.close();

              }

              catch (IOException e) {

                  System.err.println(e);

              }

          }

      }

本例根据小写字母(字母a-z)进行标记。如果由您自己实现了其等效程序,可能看上去象这样:


      import java.io.*;



      public class token2 {

          public static void main(String args[]) {

              if (args.length != 1) {

                  System.err.println("missing filename");

                  System.exit(1);

              }

              try {



                  FileReader fr = new FileReader(args[0]);

                  BufferedReader br = new BufferedReader(fr);

                  int maxlen = 256;

                  int currlen = 0;

                  char wordbuf[] = new char[maxlen];

                  int c;

                  do {

                      c = br.read();

                      if (c >= 'a' && c <= 'z') {

                          if (currlen == maxlen) {

                              maxlen *= 1.5;

                              char xbuf[] =

                                  new char[maxlen];

                              System.arraycopy( wordbuf, 0,

                                              xbuf, 0, currlen);

                              wordbuf = xbuf;

                          }

                          wordbuf[currlen++] = (char)c;

                      }

                      else

                          if (currlen > 0) {

                              String s = new String(wordbuf, 0,

                                              currlen);

                              // do something with s

                              currlen = 0;

                          }

                  } while (c != -1);

                  br.close();

              }

              catch (IOException e) {

                  System.err.println(e);

              }

          }

      }


在百度中搜索:调整JavaTM 的I/O性能(三)(zt)
在Google中搜索:调整JavaTM 的I/O性能(三)(zt)
在Yahoo中搜索:调整JavaTM 的I/O性能(三)(zt)

收藏到网摘:新浪VIVI 365key 我摘 POCO网摘 博采中心 YouNote 和讯网摘 天天收藏
[] [返回上一页] [打 印] [收 藏]

 相关文章    最新文章
· [组图] 巧用PS调整工具打造梦幻世界
· Photoshop视频教程:5.2.1 色彩调整
· [图文] 教你如何调整vista系统雅黑字体DPI..
· [图文] 调整Vista系统安全策略 让UAC更友善..
· [组图] 轻松调整DVDRip压缩电影的字幕大小..
· [组图] Photoshop图层面板调整图层图标怪问..
· [组图] Photoshop十秒钟调整曝光不足照片
· [组图] 巧用Photoshop色阶调整得到书法效果..
· [组图] Photoshop通道互换法调整照片色调
· [组图] Photoshop调整色彩饱和度欠佳的图片..
 
· 提升JSP页面响应速度的七大秘籍绝招
· 开发一个调试JSP的Eclipse插件
· JSP报表打印的一种简单解决方案
· JSP/Servlet的重定向技术综述
· java的md5加密类(zt)
· 一个用来访问http服务器的东西。功能类似..
· 菜鸟调试手记一(sql server 中文问题)
· Java性能优化技巧集锦(2)
· 用java压缩文件示例(没有中文问题)
· 使用XML/HTC/DHTML模拟标准Windows菜单

∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论…]
站内搜索

精彩图文
  网站导航  
操作系统 办公软件 网络软件
Vista Windows2003 WindowsXP Windows2000/NT Windows9X/ME Linux 其他 Word Excel Powerpoint Outlook 金山系列 其他 网页浏览 上传下载 联络聊天 邮件工具 服务器软件 网络辅助
工具软件 媒体动画 网页制作
系统工具 媒体工具 压缩工具 图文处理 文件管理 其他 3DMAX Authorware Director Maya 视频处理 其他 Flash Dreamweaver FireWorks FrontPage LiveMotion Golive HTML/CSS 其它
网站开发 平面设计 程序设计
ASP JSP PHP CGI JavaScript VBScript XML/SOAP Web服务器 Photoshop PhotoImpact CorelDraw Illustrator Freehand 设计欣赏 其他 VB VC .NET C/C++ DELPHI JAVA

冀ICP备05019428号
Copyright © 2004-2008 电脑学习网 Inc.All rights reserved.
TEL:13832340607
QQ:39873155
E_Mail:goodsgy(#)hotmail.com   (把(#)替换成@)
MSN:goodsgy(#)hotmail.com   (把(#)替换成@)