Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. public static void main(String args[]) throws IOException {
  2. // creating object of OrderedCollection
  3. OrderedCollection<String> tool = new OrderedCollection<String>();
  4. BinarySearchTree t = new BinarySearchTree();
  5. File directory = new File(directoryName);
  6. File[] directoryFile = directory.listFiles();
  7.  
  8. if (directoryFile == null) {
  9. System.out.println("This is not directory or does not Exist!");
  10. } else {
  11. System.out.print("Reading the directory " + directoryName + "."+ "\n");
  12. for (int z = 0; z < directoryFile.length; z++) {
  13.  
  14. File filename = directoryFile[z];
  15. System.out.println("Reading file " + filename + ": ");
  16.  
  17. // input streamReader and bufferedReader so user can type
  18. // InputStreamReader istream = new InputStreamReader(System.in);
  19. // BufferedReader input = new BufferedReader(istream);
  20.  
  21. char chr = ' '; // the character in an index[i] of the string
  22. // read
  23. int numOfLine = 0;
  24. int letterCounter = 0;
  25.  
  26. // contain token of words that will be going inside the
  27. // ArrayList
  28. String word = "";
  29. String totalWord = "";
  30. String pieceWord = "";
  31.  
  32. try {
  33.  
  34. FileInputStream fileStream = new FileInputStream(filename);
  35.  
  36. // Get the object of DataInputStream
  37. DataInputStream in = new DataInputStream(fileStream);
  38.  
  39. BufferedReader reader = new BufferedReader(
  40. new InputStreamReader(in));
  41.  
  42. // after reading the file's text with readLine
  43. while ((word = reader.readLine()) != null) {
  44. // by adding spaces after each line ends, it will be
  45. // useful soon
  46. word = word + " ";
  47. totalWord = totalWord + word;
  48. numOfLine++;
  49. }
  50.  
  51. // Close the input stream
  52. in.close();
  53. }
  54.  
  55. catch (Exception e) {
  56. // Catch exception if any
  57. System.err.println("Error: " + e.getMessage());
  58. }
  59.  
  60. // counts up the total letter inside the string(which contains
  61. // the file)
  62. letterCounter = totalWord.length();
  63.  
  64. int wordCounter = 0;
  65.  
  66. // loop until end of the letter(dynamically)
  67. for (int i = 0; i < letterCounter; i++) {
  68. wordCounter++;
  69.  
  70. chr = totalWord.charAt(i);
  71.  
  72. // if characters inside the string is A-Z or a-z or 0-9,
  73. // place the
  74. // word inside pieceWord
  75. if (((totalWord.charAt(i) >= 97) && (totalWord.charAt(i) <= 122))
  76. || ((totalWord.charAt(i) >= 65) && (totalWord.charAt(i) <= 90))
  77. || ((totalWord.charAt(i) >= 48) && (totalWord.charAt(i) <= 57)))
  78.  
  79. {
  80. pieceWord = pieceWord + totalWord.charAt(i);
  81. }
  82.  
  83. // if you see something that is not letter or digit come in here and
  84. // add the word
  85. else {
  86. // I saw on the output.txt file on website, everything was lowercase
  87. pieceWord = pieceWord.toLowerCase();
  88. // no repeats in arrayList and no token should be less than 1 in length
  89. // that fixed a bug where it would add an empty string every time
  90. if (pieceWord.length() >= 1)
  91. t.insert(pieceWord,filename.getName());
  92.  
  93.  
  94. // reset my "token" to empty for next "token" to make
  95. pieceWord = "";
  96.  
  97. }
  98. }// end for loop
  99. }// end for loop
  100. }// end else statement
  101.  
  102. // try to print sorted list out to file
  103. String printToFile = "cs202WordSearchOutput.txt";
  104.  
  105. FileOutputStream toFile;
  106. try {
  107. // Open an output stream
  108. toFile = new FileOutputStream(printToFile);
  109.  
  110. System.out.println("Success. " + printToFile + ", was written.");
  111.  
  112. // Print a line of text
  113. new PrintStream(toFile).println(t.find(searchWord));
  114.  
  115. // Close our output stream
  116. toFile.close();
  117. }
  118.  
  119. // catch print file errors
  120. catch (IOException e) {
  121. System.out.println("Error: " + e.getMessage() + "\nYour file was not written.");
  122. }
  123.  
  124. }// end main
  125.  
  126. }// end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement