Take the FileTweaker/FileDumper code discussed in class and add the ability to a
ID: 3531093 • Letter: T
Question
Take the FileTweaker/FileDumper code discussed in class and add the ability to append to the output file. This means changing the FileDumper class so that it can append as well as overwrite files, and changing the FileTweaker class so that it prompts the user as to whether they want to append or overwrite the output file.
I don't understand what to do here. Can you please show me what it is asking? Please!
This is the FileDumper file
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
class FileDumper {
private String path;
public FileDumper(String file_path) {
path = file_path;
}
public void dumpFile(ArrayList<String> linesToWrite) throws IOException {
FileWriter outStream = new FileWriter(path);
PrintWriter outBuffer = new PrintWriter(outStream);
//PrintWriter outBuffer = new PrintWriter(new FileWriter(path));
for (String line : linesToWrite) {
outBuffer.println(line);
}
outBuffer.close();
This is the Tweakerfile
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.IOException;
class FileTweaker {
public static void main(String[] args) throws IOException {
Scanner userInput = new Scanner(System.in);
ArrayList<String> tweakee = new ArrayList<String>();
System.out.print("Pleae enter the name of the input text file: ");
String inFile = userInput.next();
System.out.print("Pleae enter the name of the output text file: ");
String outFile = userInput.next();
try {
//FileLoader loadee = new FileLoader(inFile);
//tweakee = loadee.loadFile();
AltFileLoader altLoadee = new AltFileLoader(inFile);
tweakee = altLoadee.loadFile();
tweakee.set(2, "qux");
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
try {
FileDumper dumpee = new FileDumper(outFile);
dumpee.dumpFile(tweakee);
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
Explanation / Answer
import java.io.IOException;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
class FileDumper {
private String path;
public FileDumper(String file_path) {
path = file_path;
}
public void dumpFile(ArrayList<String> linesToWrite) throws IOException {
FileWriter outStream = new FileWriter(path);
PrintWriter outBuffer = new PrintWriter(outStream);
// PrintWriter outBuffer = new PrintWriter(new FileWriter(path));
for (String line : linesToWrite) {
outBuffer.println(line);
}
outBuffer.close();
}
public void append(ArrayList<String> linesToWrite) throws IOException {
FileWriter outStream = new FileWriter(path, true); //true indicates appending
PrintWriter outBuffer = new PrintWriter(outStream);
for (String line : linesToWrite) {
outBuffer.println(line);
}
outBuffer.close();
}
}
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class FileTweaker {
public static void main(String[] args) throws IOException {
Scanner userInput = new Scanner(System.in);
ArrayList<String> tweakee = new ArrayList<String>();
System.out.print("Pleae enter the name of the input text file: ");
String inFile = userInput.next();
System.out.print("Pleae enter the name of the output text file: ");
String outFile = userInput.next();
System.out
.print("Pleae enter whether you want to append / overwrite the file (append/overwrite): ");
String option = userInput.next();
Scanner fileScanner = new Scanner(new File(inFile));
while (fileScanner.hasNext()) {
tweakee.add(fileScanner.next());
}
FileDumper dumpee = new FileDumper(outFile);
try {
if (option.equalsIgnoreCase("append")) {
dumpee.append(tweakee);
}
if (option.equalsIgnoreCase("overwrite")) {
dumpee.dumpFile(tweakee);
}
} catch (IOException ioe) {
System.out.println(ioe.getMessage());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.