Advertisement
brilliant_moves

RandomLineFromFile.java

Jan 26th, 2015
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.88 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.BufferedReader;
  5. import java.util.Random;
  6.  
  7. public class RandomLineFromFile {
  8.  
  9.     /**
  10.     *   Program:    RandomLineFromFile.java
  11.     *   Purpose:    Print random line from specified file.
  12.     *   Version:    Java 7
  13.     *   First:      First you need to create a text file (e.g. "Phrases.txt")
  14.     *           containing a different word or sentence on each line, which this program will use.
  15.     *   Usage:      Compile this Java source code.  If using the JDK, type:
  16.     *           javac RandomLineFromFile.java
  17.     *           To Run, type
  18.     *           java RandomLineFromFile Phrases.txt
  19.     *           Then, if you want to make your batch file, type the same:
  20.     *
  21.     *           java RandomLineFromFile Phrases.txt
  22.     *
  23.     *           (where Phrases.txt is name of your text file - change this if necessary),
  24.     *           type into Notepad or other text editor and save as "RandomText.bat" or similar.
  25.     *           Run in Command Prompt. Enjoy.
  26.     *   Creator:    Chris Clarke
  27.     *   Created:    26.01.2015
  28.     */
  29.  
  30.     // declare instance variable
  31.     private static String path;
  32.  
  33.     // constructor
  34.     public RandomLineFromFile(String filePath) {
  35.         path = filePath;
  36.     }
  37.  
  38.     public String[] openFile() throws IOException {
  39.         int numberOfLines = countLines();
  40.         if (numberOfLines==0) {
  41.             System.out.println("No such file, or file contained zero lines of text.");
  42.             System.exit(1);
  43.         } // if
  44.         String[] textData = new String[numberOfLines];
  45.         FileReader fr = null;
  46.         BufferedReader textReader = null;
  47.         try {
  48.             fr = new FileReader(path);
  49.             textReader = new BufferedReader(fr);
  50.  
  51.             for(int i=0; i < numberOfLines; i++) {
  52.                 textData[i] = textReader.readLine();
  53.                 //System.out.println(textData[i]);  // display lines on screen
  54.             } // for
  55.         } catch (IOException e) {
  56.             System.out.println("Couldn't read file!");
  57.         } finally {
  58.             if (textReader!=null) {
  59.                 textReader.close();
  60.             } // if
  61.         } // try
  62.         return textData;
  63.     } // openFile()
  64.  
  65.     public int countLines() throws IOException {
  66.         int numberOfLines = 0;
  67.         FileReader readFile = null;
  68.         BufferedReader bf = null;
  69.         try {
  70.             readFile = new FileReader(path);
  71.             bf = new BufferedReader(readFile);
  72.  
  73.             String aLine;
  74.  
  75.             while (( aLine = bf.readLine()) != null) {
  76.                 numberOfLines++;
  77.             } // while
  78.         } catch (IOException e) {
  79.             System.out.println("Couldn't read file!");
  80.         } finally {
  81.             if (bf!=null) {
  82.                 bf.close();
  83.             } // if
  84.         } // try
  85.         return numberOfLines;
  86.     } // countLines()
  87.  
  88.     public static void main(String[] args) throws IOException {
  89.         String filename = "MyTextFile.txt";
  90.         if (args.length>0) {
  91.             filename = args[0];
  92.         } // if
  93.         RandomLineFromFile rl = new RandomLineFromFile(filename);
  94.         // create array of lines of text
  95.         String[] lines = rl.openFile();
  96.         // create random object
  97.         Random r = new Random();
  98.         // print a random line
  99.         System.out.println( lines[ r.nextInt( lines.length)]);
  100.     } // main()
  101.  
  102. } // class RandomLineFromFile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement