Hi,

I have written this run length encoding program to compress files. However when it is run, it is outputting each individual character with the occurrence, therefore making the file size larger!.

For example:
1. Original file - aaaaa
Compressed File - 5a
( this works like it should)

2. Original File - This is a test.
Compressed File - 1T1h1i1s1 1i1s1 1a1 1t1e1s1t1.1
(This doesn't, it makes the file larger! such be something such as: -16This is a test.)

Can anyone help me on getting strings such as these to output to the correct values.
Public String encode is where the conversion happens

Thanks
Scott

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;
import java.io.*;
 
public class RunLengthEncoding{
 
 
	public String encode(String source) {
		StringBuffer dest = new StringBuffer();
		for (int i = 0; i < source.length(); i++) {
			int runLength = 1;
			while( i+1 < source.length() && source.charAt(i) == source.charAt(i+1) ) {
				runLength++;
				i++;
			}
			dest.append(runLength);
			dest.append(source.charAt(i));
		}
		return dest.toString();
	}
 
	public static void main(String[] args) {
		Scanner dinp = new Scanner(System.in);
		String ufile;
		// the program asks the user to input the equation they would like to calculate
		System.out.println("Enter a file to compress ");
		// the input is received on the next line
		ufile = dinp.nextLine();
		String fileName = ufile + ".txt";
		Scanner inputStream = null;
		try
		{
			inputStream = new Scanner(new File(ufile));
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Error");
			System.exit(0);
		}
		while (inputStream.hasNextLine())
		{
			String line = inputStream.nextLine();
			RunLengthEncoding RLE = new RunLengthEncoding();
			String enc = (RLE.encode(line));
			System.out.println(enc);
			try
			{
				FileWriter writer = new FileWriter(ufile + "-Compressed.txt",true);
				writer.write(enc + "\n");
				writer.close();
			}
			catch(IOException e) {
 
				System.out.println("nn");
			}
		}
		inputStream.close();
	}
}