2016-04-03 26 views
-1

嗨,我是新來的java和我試圖壓縮一個字節流使用Deflaterjava.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的文件說,這些方法存在。

找不到問題出在哪裏。有人可以幫忙嗎?

+1

我在教程中在線執行完全相同的代碼,並且它完美地工作,這些方法存在於Deflate類中。 在java安裝程序中是否有錯誤,請嘗試單獨導入Inflate和Deflate。 –

回答

2

問題是你正在調用你的主類Deflater,這對編譯器來說是不明確的。有兩個同名的班級,你的班級和Zip Deflater。你應該改變這一行:Deflater compresser = new Deflater();到這個java.util.zip.Deflater compresser = new java.util.zip.Deflater();或者乾脆改變你的主類的名字。

相關問題