Hi,
Basically I have been given the task of writing a basic compression program. So far I have got what I want to print to the screen. All I need to do now if write it to a new file.
I know this is a very simple thing to do, but I cant figure it out, I keep getting various error messages. Basically I want to write to a new file what is being printed on the screen (System.out.println(RLE.encode(line))(line 43).
Any help appreciated,
Scott
import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; 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) { String fileName = "out.txt"; Scanner inputStream = null; try { inputStream = new Scanner(new File(fileName)); } catch(FileNotFoundException e) { System.out.println("Error"); System.exit(0); } while (inputStream.hasNextLine()) { String line = inputStream.nextLine(); RunLengthEncoding RLE = new RunLengthEncoding(); System.out.println(RLE.encode(line)); } inputStream.close(); } }