java.io.FileWriter duplicate characters?
<p>Given an input file (ASCII text only), compress it using a Sliding Window algorithm. Here is an outline of the steps you might follow:</p>

<pre><code>Read the file into an ArrayList of Characters

Initialize the window to the first N characters of the ArrayList where 0 &lt; N &lt; 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 
</code></pre>

<p>I think my problem is that for every character in the input file, I am writing that character twice to the output file. </p>

<p>My Code:</p>

<pre><code>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 &lt;Character&gt; comparisonArray = new ArrayList &lt;Character&gt; ();
    int ind = 0;
    for (int i = 0; i + windowSize + 1 &lt; fileArray.size(); i++) {
        for (int b = ind; b &lt;= 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) &gt;= 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 &lt; 4 &amp;&amp; i == 0) {
            for (int length = 0; length &lt; window.size(); length++) {
                try {
                    output.write(window.get(length));
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            for (int a = 0; a &lt; comparisonArray.size(); a++) {
                try {
                    output.write(comparisonArray.get(a));
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            comparisonArray.clear();
            window.clear();
        }

        else if (i == 0 &amp;&amp; marker &gt;= 4){
            for (int length = 0; length &lt; 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 &amp;&amp; marker &gt;= 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 &amp;&amp; marker &lt; 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();
}
</code></pre>
