0

给定一个输入文件(仅限 ASCII 文本),使用滑动窗口算法对其进行压缩。以下是您可能遵循的步骤的概要:

Read the file into an ArrayList of Characters

Initialize the window to the first N characters of the ArrayList where 0 < N < 256. The window can be another ArrayList or it can be index markers that point to a beginning and ending position within the main ArrayList. You get to choose your approach.

See if the next byte after the window is contained in the window.

If so, then
    Scan forward and keep track of how many chars match
    Go to next occurance of first byte
    Scan forward and keep track of how many chars match
    Do this for all occurances of first byte within the window and use the position that has the highest match length.
    Write special marker, position, and match length to output file 

If not, then
    Write next byte to output file 

Slide the window forward either by one character or match length, depending on what you found in step 3

Close the output file 

我认为我的问题是对于输入文件中的每个字符,我将该字符两次写入输出文件。

我的代码:

public void compressAndSaveAs() throws IOException {
    //new FileWriter
    FileWriter output = new FileWriter("MuffinManCompressed.txt");
    (get length of window from user and set equal to windowSize)
    //creates an array to compare with the window array
    ArrayList <Character> comparisonArray = new ArrayList <Character> ();
    int ind = 0;
    for (int i = 0; i + windowSize + 1 < fileArray.size(); i++) {
        for (int b = ind; b <= windowSize + ind; b++) {
            window.add(fileArray.get(b));
        }
        ind++;
        comparisonArray.add(fileArray.get(i + windowSize + 1));
        int placeHolder = i;
        int marker = 0;
        i = 0;
        char test = comparisonArray.get(i);
        while (window.contains(test)) {
            i++;
            if ((i + windowSize + 1) >= fileArray.size()) {
                break;
            }
            comparisonArray.add(fileArray.get(i + windowSize + 1));
            test = comparisonArray.get(i);
            marker++;
        }
        i = placeHolder;
        System.out.print(i);
        System.out.println(window);
        if (marker < 4 && i == 0) {
            for (int length = 0; length < window.size(); length++) {
                try {
                    output.write(window.get(length));
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            for (int a = 0; a < comparisonArray.size(); a++) {
                try {
                    output.write(comparisonArray.get(a));
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            comparisonArray.clear();
            window.clear();
        }

        else if (i == 0 && marker >= 4){
            for (int length = 0; length < window.size(); length++) {
                try {
                    output.write(window.get(length));
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                output.write((int) 36);
                String firstFactor = Integer.toString(window.indexOf(comparisonArray.get(0)));
                String secondFactor = Integer.toString(comparisonArray.size());
                output.write(firstFactor);
                output.write(", ");
                output.write(secondFactor);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            comparisonArray.clear();
            window.clear();
        }

        else if (i != 0 && marker >= 4){

                try {
                    output.write(window.get(window.size() - 1));
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }

            try {
                output.write((int) 36);
                String firstFactor = Integer.toString(window.indexOf(comparisonArray.get(0)));
                String secondFactor = Integer.toString(comparisonArray.size());
                output.write(firstFactor);
                output.write(", ");
                output.write(secondFactor);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            comparisonArray.clear();
            window.clear();
        }
        else if (i != 0 && marker < 4){

                try {
                    output.write(window.get(window.size() - 1));
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    output.write(comparisonArray.get(comparisonArray.size() - 1));
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            comparisonArray.clear();
            window.clear();
        }
    output.close();
}
4

0 回答 0