Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Writing Output To New File

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Writing Output To New File

    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();
     
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Writing Output To New File

    See this post: How to write read binary data. Depending upon how your encryption works, you may wish to use a DataOuptutStream or something like a BufferedWriter. Given the System prints what you expect, try using a FileWriter and BufferedWriter to write to the output file. Google these and you'll find lots of examples on how to use them

  3. The Following User Says Thank You to copeg For This Useful Post:

    Scottj996 (January 7th, 2010)

Similar Threads

  1. Writing java applications for the iPod Touch
    By JavaPF in forum Java ME (Mobile Edition)
    Replies: 8
    Last Post: June 14th, 2011, 04:34 PM
  2. Reading and Writing Text Files
    By kappasig84 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 1st, 2010, 07:16 PM
  3. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 08:41 AM
  4. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM
  5. I need help writing this program
    By kev2000 in forum Algorithms & Recursion
    Replies: 5
    Last Post: June 4th, 2009, 03:14 AM