import java.io.*; import java.util.*; import java.net.*; public class StringChooser { private static Randomizer rzer = new Randomizer(System.currentTimeMillis()); //private static Randomizer rzer = new Randomizer(); public static String chooseLine (String filename) { try { /* Applets generally can't open files on a machine (for security reasons). * To achieve this, we have to send the IP (which matches that of the * code source. */ URL source = new URL(filename); BufferedReader br = new BufferedReader(new InputStreamReader(source.openStream())); StreamTokenizer st = new StreamTokenizer(br); st.wordChars('\u0000', '\u00FF'); st.whitespaceChars('\n', '\n'); // newline char; ((int) '\n') == 10 st.whitespaceChars('\r', '\r'); // carriage return char; ((int) '\r') == 13 Vector v = new Vector(); while (! (st.nextToken() == StreamTokenizer.TT_EOF)) { v.addElement(st.sval); } return (String) (v.elementAt(rzer.intBetween(0,v.size() - 1))); } catch (MalformedURLException e) { //System.out.println("*** StringChoooser Error: File Not Found " //+ filename //+ " ***"); throw new RuntimeException("File Not Found: " + filename); } catch (SecurityException e) { //System.out.println("*** StringChoooser Error: File Not Found " //+ filename //+ " ***"); throw new RuntimeException("Lacking security permission to open file"); } catch (IOException e) { //System.out.println("*** StringChoooser Error: IO Exception in file " //+ filename //+ "\n" //+ e.getMessage() //+ " ***"); throw new RuntimeException("IO Exception: " + e.getMessage()); } } }