Class Writer

Object
Writer

public class Writer extends Object
This class is where you will be writing to a newly created file.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) PrintWriter
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Writer(String fileName)
    The FileOutput constructor takes in a single String parameter, which is the name of the file we would like to create.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    This method appends an end message to the PrintWriter object and then closes the PrintWriter to ensure no memory leaks occur.
    void
    This method works through each element contained in the provided ArrayList and determines if duplicate elements exist within the ArrayList.
    void
    logMax(ArrayList<String> fileContents)
    This method works through each element in the provided ArrayList and determines the largest number that is contained within said ArrayList.
    void
    logReverse(ArrayList<String> fileContents)
    This method should work through the ArrayList provided backwards, and print each element to our file backwards, using the PrintWriter initialized in the constructor.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • Writer

      public Writer(String fileName) throws IOException
      The FileOutput constructor takes in a single String parameter, which is the name of the file we would like to create. Using this parameter, create a FileOutputStream and set the PrintWriter object, outputFile, using the newly created FileOutputStream. The additional information section on the README may assist you if any problems arise. This PrintWriter object will be used for the rest of our functions, logReverse(), logMax(), logDuplicates().
      Parameters:
      fileName - Desired name for the file to be created.
      Throws:
      IOException - In the case that any errors arise when we are creating our new file, we practically throw them away.
  • Method Details

    • closeWriter

      public void closeWriter()
      This method appends an end message to the PrintWriter object and then closes the PrintWriter to ensure no memory leaks occur.
    • logReverse

      public void logReverse(ArrayList<String> fileContents)
      This method should work through the ArrayList provided backwards, and print each element to our file backwards, using the PrintWriter initialized in the constructor. It should not matter whether the contents of the file are numbers, Strings, or sentences. The purpose of this method is to reverse the contents of the fileContents ArrayList and then print them to our new file. For example:
      
       12
       42
       19
       12
       58
       
      The new file would contain:
      
       Reversed file contents: 
       58
       12
       19
       42
       12
       
      Parameters:
      fileContents - ArrayList containing all lines of a file.
    • logMax

      public void logMax(ArrayList<String> fileContents)
      This method works through each element in the provided ArrayList and determines the largest number that is contained within said ArrayList. Whichever number is determined to be the largest will be logged, using the PrintWriter initialized in the constructor. You may have noticed that the ArrayList is populated by String objects, so using a Wrapper class to convert from String to int will come in use here. If the provided file contained:
      
       12
       42
       19
       12
       58
       
      The new file would contain:
      
       The largest number in the file is: 58
       
      Parameters:
      fileContents - ArrayList containing all lines of a file, all elements will be whole, positive numbers when this method is tested.
    • logDuplicates

      public void logDuplicates(ArrayList<String> fileContents)
      This method works through each element contained in the provided ArrayList and determines if duplicate elements exist within the ArrayList. If duplicate elements are found, print to file, "true", or "false" if they are not. There are multiple solutions to this problem, but one of the most common approaches involves a nested loop. If the provided file contained:
      
       12
       42
       19
       12
       58
       
      The new file would contain:
      
       Duplicates found: true
       
      Parameters:
      fileContents - ArrayList