Crenox

Thesaurus Reader Java Program

Mar 16th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.Scanner;
  4.  
  5. public class ThesaurusReader
  6. {
  7. public static void main(String[] args) throws Exception
  8. {
  9. String word = "Substantiality";
  10. String wordTwo = "#";
  11. String fileName = "files/1/thesaurus.txt";
  12. String line = null;
  13. int lineNum = 0, lineStart = 0, lineEnd = 0;
  14.  
  15. try
  16. {
  17. /* FOR URLS
  18. URL url = new URL("http://www.thesaurus.com/browse/" + word);
  19. Scanner sc = new Scanner(url.openStream());
  20.  
  21. lineNum++;
  22. System.out.println("LINE " + lineNum + ": " + sc.nextLine());
  23. lineNum++;
  24. System.out.println("LINE " + lineNum + ": " + sc.nextLine());
  25. lineNum++;
  26. System.out.println("LINE " + lineNum + ": " + sc.nextLine());
  27. lineNum++;
  28. System.out.println("LINE " + lineNum + ": " + sc.nextLine());
  29. */
  30.  
  31. // FileReader reads text files in the default encoding.
  32. FileReader fr = new FileReader(fileName);
  33. // Always wrap FileReader in BufferedReader.
  34. BufferedReader br = new BufferedReader(fr);
  35.  
  36. // reads text file
  37. while((line = br.readLine()) != null)
  38. {
  39. lineNum++;
  40. System.out.println(/*"LINE " + lineNum + ": " +*/ line);
  41.  
  42. lineStart = wordTwo.charAt(0);
  43. //System.out.println(lineStart);
  44. if(line.equals("#1. Existence."))
  45. {
  46. br.close();
  47. }
  48. }
  49.  
  50. // Always close files.
  51. br.close();
  52. }
  53. catch(FileNotFoundException ex)
  54. {
  55. System.out.println("Unable to open file '" + fileName + "'");
  56. }
  57. catch(IOException ex)
  58. {
  59. System.out.println("Error reading file '" + fileName + "'");
  60. // Or we could just do this:
  61. // ex.printStackTrace();
  62. }
  63. }
  64. }
  65. /*
  66. RESOURCES
  67. http://www.tutorialspoint.com/compile_java_online.php - online compiler
  68. http://www.thesaurus.com/ - thesaurus website
  69.  
  70. ANSWERS
  71. https://stackoverflow.com/questions/5600422/method-to-find-string-inside-of-the-text-file-then-getting-the-following-lines
  72. https://stackoverflow.com/questions/20418319/find-a-string-or-a-line-in-a-txt-file-java
  73. */
Add Comment
Please, Sign In to add comment