我的放气器 class 工作不正常

My deflater class works incorecctly

我的压缩 class 工作不正常。当我尝试压缩包含句子 "something" 的简单文件时,压缩和未压缩 returns 其他内容。这是我的放气代码:

public static void inflate(String arg) throws Exception {
        try {
            FileInputStream fin = new FileInputStream(arg);
            InflaterInputStream in = new InflaterInputStream(fin);

            FileOutputStream fout = new FileOutputStream("def.txt");

            int i;
            while ((i = in.read()) != -1) {
                fout.write((byte) i);
                fout.flush();
            }

            fin.close();
            fout.close();
            in.close();

        } catch (Exception e) {
            System.out.println(e);
        }
        new File(arg).delete();
        new File("def.txt").renameTo(new File(arg));
    }

    public static void deflate(String arg) throws Exception {
        try {
            FileInputStream fin = new FileInputStream(arg);

            FileOutputStream fout = new FileOutputStream("def.txt");
            DeflaterOutputStream out = new DeflaterOutputStream(fout);

            int i;
            while ((i = fin.read()) != -1) {
                out.write((byte) i);
                out.flush();
            }

            fin.close();
            out.close();

        } catch (Exception e) {
            System.out.println(e);
        }
        new File(arg).delete();
        new File("def.txt").renameTo(new File(arg));

    }

我用

来称呼它
public static void main(String[] args) {
        try {
            Main.deflate(args[0]);
            Main.inflate(args[0]);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

那么如何修复我的代码?我认为问题不在于压缩代码。

您的代码似乎确实按预期工作。

运行 它在包含单词 'something' 的文本文件上 returns 是一个相同的文件。 要确认输出相同,请尝试编辑以下行:

Test.inflate("def.txt");

这是你的主要功能,

FileOutputStream fout = new FileOutputStream("output.txt");

来自你的膨胀函数。

然后在您的 deflate() 和 inflate() 函数中注释掉以下行

//new File(arg).delete();
//new File("def.txt").renameTo(new File(arg));

程序现在将获取一个输入文件,根据您的示例,我使用 input.txt 和单词 'something',并创建一个压缩文件 def.txt 和一个通过扩充 def.txt[=31 创建的 output.txt 文件=].

输出文件应与输入文件完全匹配,而压缩后的文件应不同。如果没有,则必须有一些关于缺少的程序的进一步信息。