Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String args[]) throws IOException {
- // creating object of OrderedCollection
- OrderedCollection<String> tool = new OrderedCollection<String>();
- BinarySearchTree t = new BinarySearchTree();
- File directory = new File(directoryName);
- File[] directoryFile = directory.listFiles();
- if (directoryFile == null) {
- System.out.println("This is not directory or does not Exist!");
- } else {
- System.out.print("Reading the directory " + directoryName + "."+ "\n");
- for (int z = 0; z < directoryFile.length; z++) {
- File filename = directoryFile[z];
- System.out.println("Reading file " + filename + ": ");
- // input streamReader and bufferedReader so user can type
- // InputStreamReader istream = new InputStreamReader(System.in);
- // BufferedReader input = new BufferedReader(istream);
- char chr = ' '; // the character in an index[i] of the string
- // read
- int numOfLine = 0;
- int letterCounter = 0;
- // contain token of words that will be going inside the
- // ArrayList
- String word = "";
- String totalWord = "";
- String pieceWord = "";
- try {
- FileInputStream fileStream = new FileInputStream(filename);
- // Get the object of DataInputStream
- DataInputStream in = new DataInputStream(fileStream);
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(in));
- // after reading the file's text with readLine
- while ((word = reader.readLine()) != null) {
- // by adding spaces after each line ends, it will be
- // useful soon
- word = word + " ";
- totalWord = totalWord + word;
- numOfLine++;
- }
- // Close the input stream
- in.close();
- }
- catch (Exception e) {
- // Catch exception if any
- System.err.println("Error: " + e.getMessage());
- }
- // counts up the total letter inside the string(which contains
- // the file)
- letterCounter = totalWord.length();
- int wordCounter = 0;
- // loop until end of the letter(dynamically)
- for (int i = 0; i < letterCounter; i++) {
- wordCounter++;
- chr = totalWord.charAt(i);
- // if characters inside the string is A-Z or a-z or 0-9,
- // place the
- // word inside pieceWord
- if (((totalWord.charAt(i) >= 97) && (totalWord.charAt(i) <= 122))
- || ((totalWord.charAt(i) >= 65) && (totalWord.charAt(i) <= 90))
- || ((totalWord.charAt(i) >= 48) && (totalWord.charAt(i) <= 57)))
- {
- pieceWord = pieceWord + totalWord.charAt(i);
- }
- // if you see something that is not letter or digit come in here and
- // add the word
- else {
- // I saw on the output.txt file on website, everything was lowercase
- pieceWord = pieceWord.toLowerCase();
- // no repeats in arrayList and no token should be less than 1 in length
- // that fixed a bug where it would add an empty string every time
- if (pieceWord.length() >= 1)
- t.insert(pieceWord,filename.getName());
- // reset my "token" to empty for next "token" to make
- pieceWord = "";
- }
- }// end for loop
- }// end for loop
- }// end else statement
- // try to print sorted list out to file
- String printToFile = "cs202WordSearchOutput.txt";
- FileOutputStream toFile;
- try {
- // Open an output stream
- toFile = new FileOutputStream(printToFile);
- System.out.println("Success. " + printToFile + ", was written.");
- // Print a line of text
- new PrintStream(toFile).println(t.find(searchWord));
- // Close our output stream
- toFile.close();
- }
- // catch print file errors
- catch (IOException e) {
- System.out.println("Error: " + e.getMessage() + "\nYour file was not written.");
- }
- }// end main
- }// end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement