Class Writer
Object
Writer
This class is where you will be writing to a newly created file.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidThis method appends an end message to the PrintWriter object and then closes the PrintWriter to ensure no memory leaks occur.voidlogDuplicates(ArrayList<String> fileContents) This method works through each element contained in the provided ArrayListand determines if duplicate elements exist within the ArrayList. voidThis method works through each element in the provided ArrayList and determines the largest number that is contained within said ArrayList.voidlogReverse(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.
-
Field Details
-
outputFile
PrintWriter outputFile
-
-
Constructor Details
-
Writer
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
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:
The new file would contain:12 42 19 12 58Reversed file contents: 58 12 19 42 12- Parameters:
fileContents- ArrayListcontaining all lines of a file.
-
logMax
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:
The new file would contain:12 42 19 12 58The largest number in the file is: 58- Parameters:
fileContents- ArrayListcontaining all lines of a file, all elements will be whole, positive numbers when this method is tested.
-
logDuplicates
This method works through each element contained in the provided ArrayListand 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:
The new file would contain:12 42 19 12 58Duplicates found: true- Parameters:
fileContents- ArrayList
-