人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

調整JavaTM 的I/O性能(3)(zt)

調整JavaTM 的I/O性能(3)(zt)

更新時間:2022-10-11 文章作者:未知 信息來源:網絡 閱讀次數:

壓縮


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);

}

}

}

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 浠水县| 祁连县| 望江县| 丰城市| 类乌齐县| 阳城县| 九龙城区| 石嘴山市| 霸州市| 肃南| 泸溪县| 大宁县| 海门市| 紫云| 綦江县| 徐州市| 福泉市| 东明县| 大足县| 福安市| 新余市| 万源市| 东乌| 垣曲县| 霍山县| 黔江区| 建昌县| 普安县| 青海省| 阳春市| 新丰县| 筠连县| 汶上县| 博兴县| 松滋市| 葫芦岛市| 岐山县| 商洛市| 茶陵县| 钦州市| 玉龙|