Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Learn Java from Ben Franklin

Write better Java code using Franklin's learning techniques

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 4

catch
PrintWriter object
close BufferedWriter object
FileWriter object
Write one lame joke to file
close PrintWriter object
main()
BufferedWriter object
close FileWriter object
try


To help create random lists, the following program takes a text file and creates a new file with the original file's lines in random order. The program should be run from the command prompt in the form of "Scrambler file1.ext file2.ext." Here file1.ext is the original file, and file2.ext is the randomized file. If the output file already exists, it will be overwritten without any warning, so take care in choosing an output filename.

import java.io.*;
import java.util.*;
public class Scrambler {
   public static void main(String[] args) {
      if (args.length < 2) {
         System.out.println("Must have two arguments in the form of:");
         System.out.println("Scrambler file1.ext file2.ext");
      }
      else {
         String inputFile = args[0];
         String outputFile = args[1];
         Scrambler sc = new Scrambler();
         sc.scramble(inputFile, outputFile);
      }
   }
   public void scramble(String input, String output) {
      try {
         FileReader     fr = new FileReader(input);
         BufferedReader br = new BufferedReader(fr);
         PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(output)));
         List list = new ArrayList();
         String text;
         text = br.readLine();
         while (text != null) {
            list.add(text);
            text = br.readLine();
         }
         fr.close();
         br.close();
         int x = list.size();
         Collections.shuffle(list);
         for ( int I = 0 ; I < x ; I++) {
            String s = (String) list.get(I);
            System.out.println(s);
            pw.println(s);
         }
         pw.close();
      }
      catch(Exception e) {
         System.out.println(e.toString());
      }
   }
}


If you can understand the general workings of the above program, you may want to make a list of this program's summaries for your own Java learning exercise.

Transforming exercises

Another method Franklin used was to change text to verse (poetry), lay it aside for several days, and then change the verse back to text. You can do similar exercises with Java -- for example, you can:

  • Change applets to applications
  • Change programs that read and write to files from a Reader/Writer model to programs that use InputStream/OutputStream
  • Change threaded applications that subclass Thread to those that create a class that implements Runnable


Exercise 3: Combining declarations

Let's start with a simple example. When creating a chain of input or output streams, you can create separate objects that get chained, as in the first program listing:

         FileWriter fw = new FileWriter("test.txt");
         BufferedWriter bw = new BufferedWriter(fw);
         PrintWriter pw = new PrintWriter(bw);


Alternatively, you can combine all the declarations into one line. One exercise, then, would be to change the first program into one whose chain of output streams is declared in one line. A sample solution is given below:

import java.io.*;
public class WriteTest {
   public static void main(String[] args) {
      try {
         PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("test2.txt")));
         pw.println("This is a test");
         pw.println("This is only a test");
         pw.println("In the event of a real emergency it would be too late");
         pw.close();
      }
      catch (Exception e) {
         System.out.println(e.toString());
      }
   }
}     


Exercise 4: Writer and OutputStream

Another exercise you can do is to change a program that uses the Writer model to one that uses the OutputStream model, yet produces the same output. Then, set the OutputStream model aside for some days before changing it back to a program using the original Writer model. Starting with the version of the program immediately above, you might come up with:

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Also by Robert Nielsen: