我正在尝试实现算术编码,这是一种压缩算法。这是压缩代码。当我编译时
线程中的异常"主" 在arithmetic.arithmeticcompress.main(arithmeticcompress.java:35)
它给了我这个错误。
来源
package arithmetic;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ArithmeticCompress {
public static void main(String[] args) throws IOException {
File inputFile = new File("D:\5.txt");
File outputFile = new File("D:\new1.txt");
// Read input file once to compute symbol frequencies
//Line no 35
FrequencyTable freqs = getFrequencies(inputFile);
freqs.increment(256); // EOF symbol gets a frequency of 1
// Read input file again, compress with arithmetic coding, and write output file
try (InputStream in = new BufferedInputStream(new FileInputStream(inputFile))) {
try (BitOutputStream out = new BitOutputStream(new BufferedOutputStream(new FileOutputStream(outputFile)))) {
writeFrequencies(out, freqs);
compress(freqs, in, out);
}
}
}
// Returns a frequency table based on the bytes in the given file.
// Also contains an extra entry for symbol 256, whose frequency is set to 0.
private static FrequencyTable getFrequencies(File file) throws IOException {
FrequencyTable freqs = new SimpleFrequencyTable(new int[257]);
try (InputStream input = new BufferedInputStream(new FileInputStream(file))) {
while (true) {
int b = input.read();
if (b == -1)
break;
freqs.increment(b);
}
}
return freqs;
}
// To allow unit testing, this method is package-private instead of private.
static void writeFrequencies(BitOutputStream out, FrequencyTable freqs) throws IOException {
for (int i = 0; i < 256; i++)
writeInt(out, 32, freqs.get(i));
}
// To allow unit testing, this method is package-private instead of private.
static void compress(FrequencyTable freqs, InputStream in, BitOutputStream out) throws IOException {
ArithmeticEncoder enc = new ArithmeticEncoder(out);
while (true) {
int symbol = in.read();
if (symbol == -1)
break;
enc.write(freqs, symbol);
}
enc.write(freqs, 256); // EOF
enc.finish(); // Flush remaining code bits
}
// Writes an unsigned integer of the given bit width to the given stream.
private static void writeInt(BitOutputStream out, int numBits, int value) throws IOException {
if (numBits < 0 || numBits > 32)
throw new IllegalArgumentException();
for (int i = numBits - 1; i >= 0; i--)
out.write((value >>> i) & 1);
}
}
好吧,这有点奇怪,因为这种源代码甚至不应该编译(因此不应该有任何Runtime Exception):因为您的代码是参考类频率图,但该类不是在您的代码中定义,甚至没有导入?没有把握。但是通常,这将导致汇编错误,并且您应该无法运行此类代码 - 在这里,我想您正在使用某些IDE(例如Netbeans,也许是Eclipse),即使它不使用,也可以运行代码t编译 ->但在拉车期间会失败。
我建议在IDE中完全重建(清洁和编译)您的项目以显示您在代码中可能遇到的错误...