Examples of smaller Java programs that do various interesting things.

12345678910111213141516171819202122232425
  1. //sample code to write 100 random ints to a file, 1 per line
  2. import java.io.PrintStream;
  3. import java.io.IOException;
  4. import java.io.File;
  5. import java.util.Random;
  6. public class WriteToFile
  7. { public static void main(String[] args)
  8. { try
  9. { PrintStream writer = new PrintStream( new File("randInts.txt"));
  10. Random r = new Random();
  11. final int LIMIT = 100;
  12. for(int i = 0; i < LIMIT; i++)
  13. { writer.println( r.nextInt() );
  14. }
  15. writer.close();
  16. }
  17. catch(IOException e)
  18. { System.out.println("An error occured while trying to write to the file");
  19. }
  20. }
  21. }