Change the code in Writer.java only to get it working. Must contain methods: logReverse() ,
logMax(), logDuplicates(),
This lab is going to focus on File Output, which you will find is somewhat similar to console
output.
FileMain.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class FileMain {
public static void main(String[] args) throws IOException{
Scanner scnr = new Scanner(System.in);
System.out.print("Please enter the name of the file you would like to read: ");
String fileName = scnr.next();
Reader reader = new Reader();
ArrayList fileContents = reader.getFileContents(fileName);
System.out.println("Please enter a name for your new file: ");
String newFileName = scnr.next();
Writer fileOut = new Writer(newFileName);
fileOut.logReverse(fileContents);
fileOut.logMax(fileContents);
fileOut.logDuplicates(fileContents);
fileOut.closeWriter();
scnr.close();
}
}
Filetester.java
import java.io.IOException;
import java.util.ArrayList;
public class FileTester {
public static boolean testLogReverse(ArrayList contents) throws IOException {
Writer writer = new Writer("logReverseTest.txt");
writer.logReverse(contents);
writer.closeWriter();
ArrayList expected = new ArrayList();
expected.add("Reversed file contents: ");
expected.add("58");
expected.add("12");
expected.add("19");
expected.add("42");
expected.add("12");
expected.add("End of file.");
Reader testReader = new Reader();
ArrayList result = testReader.getFileContents("logReverseTest.txt");
if(expected.equals(result)) return true;
else return false;
}
public static boolean testLogMax(ArrayList contents) throws IOException {
Writer writer = new Writer("logMaxTest.txt");
writer.logMax(contents);
writer.closeWriter();
ArrayList expected = new ArrayList();
expected.add("The largest number in the file is: 58");
expected.add("End of file.");
Reader testReader = new Reader();
ArrayList result = testReader.getFileContents("logMaxTest.txt");
if(expected.equals(result)) return true;
else return false;
}
public static boolean testLogDuplicates(ArrayList contents) throws IOException {
Writer writer = new Writer("logDuplicatesTest.txt");
writer.logDuplicates(contents);
writer.closeWriter();
ArrayList expected = new ArrayList();
expected.add("Duplicates found: true");
expected.add("End of file.");
Reader testReader = new Reader();
ArrayList result = testReader.getFileContents("logDuplicatesTest.txt");
if(expected.equals(result)) return true;
else return false;
}
public static void main(String[] args) throws IOException {
Reader reader = new Reader();
ArrayList fileContents = reader.getFileContents("nums.txt");
System.out.println("logReverse test passed? " + testLogReverse(fileContents));
System.out.println("logMax test passed? " + testLogMax(fileContents));
System.out.println("logDuplicates test passed? " + testLogDuplicates(fileContents));
}
}
Reader.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Sca.
Change the code in Writer.java only to get it working. Must contain .pdf
1. Change the code in Writer.java only to get it working. Must contain methods: logReverse() ,
logMax(), logDuplicates(),
This lab is going to focus on File Output, which you will find is somewhat similar to console
output.
FileMain.java
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class FileMain {
public static void main(String[] args) throws IOException{
Scanner scnr = new Scanner(System.in);
System.out.print("Please enter the name of the file you would like to read: ");
String fileName = scnr.next();
Reader reader = new Reader();
ArrayList fileContents = reader.getFileContents(fileName);
System.out.println("Please enter a name for your new file: ");
String newFileName = scnr.next();
Writer fileOut = new Writer(newFileName);
fileOut.logReverse(fileContents);
fileOut.logMax(fileContents);
fileOut.logDuplicates(fileContents);
fileOut.closeWriter();
scnr.close();
}
}
Filetester.java
import java.io.IOException;
import java.util.ArrayList;
public class FileTester {
public static boolean testLogReverse(ArrayList contents) throws IOException {
Writer writer = new Writer("logReverseTest.txt");
writer.logReverse(contents);
writer.closeWriter();
ArrayList expected = new ArrayList();
2. expected.add("Reversed file contents: ");
expected.add("58");
expected.add("12");
expected.add("19");
expected.add("42");
expected.add("12");
expected.add("End of file.");
Reader testReader = new Reader();
ArrayList result = testReader.getFileContents("logReverseTest.txt");
if(expected.equals(result)) return true;
else return false;
}
public static boolean testLogMax(ArrayList contents) throws IOException {
Writer writer = new Writer("logMaxTest.txt");
writer.logMax(contents);
writer.closeWriter();
ArrayList expected = new ArrayList();
expected.add("The largest number in the file is: 58");
expected.add("End of file.");
Reader testReader = new Reader();
ArrayList result = testReader.getFileContents("logMaxTest.txt");
if(expected.equals(result)) return true;
else return false;
}
public static boolean testLogDuplicates(ArrayList contents) throws IOException {
Writer writer = new Writer("logDuplicatesTest.txt");
writer.logDuplicates(contents);
writer.closeWriter();
ArrayList expected = new ArrayList();
expected.add("Duplicates found: true");
expected.add("End of file.");
Reader testReader = new Reader();
ArrayList result = testReader.getFileContents("logDuplicatesTest.txt");
3. if(expected.equals(result)) return true;
else return false;
}
public static void main(String[] args) throws IOException {
Reader reader = new Reader();
ArrayList fileContents = reader.getFileContents("nums.txt");
System.out.println("logReverse test passed? " + testLogReverse(fileContents));
System.out.println("logMax test passed? " + testLogMax(fileContents));
System.out.println("logDuplicates test passed? " + testLogDuplicates(fileContents));
}
}
Reader.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Reader {
private Scanner getFileScanner(String fileName) {
try {
File file = new File(fileName);
Scanner scnr = new Scanner(file);
return scnr;
} catch(FileNotFoundException e) {
System.err.println(""" + fileName + "" not found, please ensure that the file trying to be
read is in the folder just above the src directory.");;
System.err.println("Program now exiting!");
System.exit(-1);
}
return null;
}
public ArrayList getFileContents(String fileName) {
Scanner fileScnr = getFileScanner(fileName);
ArrayList fileContents = new ArrayList();
4. while(fileScnr.hasNextLine()) {
fileContents.add(fileScnr.nextLine());
}
return fileContents;
}
}
Writer.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Writer {
PrintWriter outputFile;
public Writer(String fileName) throws IOException{
//TODO Student
}
public void closeWriter() {
outputFile.print("End of file.");
outputFile.close();
}
/** Student Self-Explanation
*
*/
public void logReverse(ArrayList fileContents) {
outputFile.println("Reversed file contents: ");
//TODO Student
}
/** Student Self-Explanation
*
*/
public void logMax(ArrayList fileContents) {
//TODO Student
outputFile.print("The largest number in the file is: ");
//You will want to print your max number on this line.
}
5. /** Student Self-Explanation
*
*/
public void logDuplicates(ArrayList fileContents) {
outputFile.print("Duplicates found: ");
//TODO Student
}
}
Java doc for writer.java:
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 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:
The new file would contain:
Parameters:
fileContents - ArrayList containing all lines of a file.
logMax
public void logMax(ArrayList 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:
The new file would contain:
Parameters:
fileContents - ArrayList containing all lines of a file, all elements will be whole, positive
numbers when this method is tested.
6. logDuplicates
public void logDuplicates(ArrayList 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:
The new file would contain:
Parameters:
fileContents - ArrayList