Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.util.Scanner;
- public class ThesaurusReader
- {
- public static void main(String[] args) throws Exception
- {
- String word = "Substantiality";
- String wordTwo = "#";
- String fileName = "files/1/thesaurus.txt";
- String line = null;
- int lineNum = 0, lineStart = 0, lineEnd = 0;
- try
- {
- /* FOR URLS
- URL url = new URL("http://www.thesaurus.com/browse/" + word);
- Scanner sc = new Scanner(url.openStream());
- lineNum++;
- System.out.println("LINE " + lineNum + ": " + sc.nextLine());
- lineNum++;
- System.out.println("LINE " + lineNum + ": " + sc.nextLine());
- lineNum++;
- System.out.println("LINE " + lineNum + ": " + sc.nextLine());
- lineNum++;
- System.out.println("LINE " + lineNum + ": " + sc.nextLine());
- */
- // FileReader reads text files in the default encoding.
- FileReader fr = new FileReader(fileName);
- // Always wrap FileReader in BufferedReader.
- BufferedReader br = new BufferedReader(fr);
- // reads text file
- while((line = br.readLine()) != null)
- {
- lineNum++;
- System.out.println(/*"LINE " + lineNum + ": " +*/ line);
- lineStart = wordTwo.charAt(0);
- //System.out.println(lineStart);
- if(line.equals("#1. Existence."))
- {
- br.close();
- }
- }
- // Always close files.
- br.close();
- }
- catch(FileNotFoundException ex)
- {
- System.out.println("Unable to open file '" + fileName + "'");
- }
- catch(IOException ex)
- {
- System.out.println("Error reading file '" + fileName + "'");
- // Or we could just do this:
- // ex.printStackTrace();
- }
- }
- }
- /*
- RESOURCES
- http://www.tutorialspoint.com/compile_java_online.php - online compiler
- http://www.thesaurus.com/ - thesaurus website
- ANSWERS
- https://stackoverflow.com/questions/5600422/method-to-find-string-inside-of-the-text-file-then-getting-the-following-lines
- https://stackoverflow.com/questions/20418319/find-a-string-or-a-line-in-a-txt-file-java
- */
Add Comment
Please, Sign In to add comment