-1
嗨,我是新來的java和我試圖壓縮一個字節流使用Deflater
在java.util.zip
。我跟着一個例子從Oracle site。如何在java.util.zip中使用Deflater
try {
// Encode a String into bytes
String inputString = "blahblahblah";
byte[] input = inputString.getBytes("UTF-8");
// Compress the bytes
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
compresser.end();
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
} catch(java.io.UnsupportedEncodingException ex) {
// handle
} catch (java.util.zip.DataFormatException ex) {
// handle
}
當我運行這段代碼是給了我一個錯誤說setInput()
,finish()
,deflate()
和end()
沒有定義。以下是錯誤消息
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method setInput(byte[]) is undefined for the type Deflater
The method finish() is undefined for the type Deflater
The method deflate(byte[]) is undefined for the type Deflater
The method end() is undefined for the type Deflater
at Deflater.main(Deflater.java:16)
我進口java.util.zip
看着在甲骨文site.It的文件說,這些方法存在。
找不到問題出在哪裏。有人可以幫忙嗎?
我在教程中在線執行完全相同的代碼,並且它完美地工作,這些方法存在於Deflate類中。 在java安裝程序中是否有錯誤,請嘗試單獨導入Inflate和Deflate。 –