Complete the Java program below so that it reads the text file dogNames.txt and
ID: 649837 • Letter: C
Question
Complete the Java program below so that it reads the text file dogNames.txt and writes its contents to another text file, "doggoneData.txt".
The file dogNames.txt contains just the single word "Tippy", so the output in "doggoneData.txt" should simply look like this:
Tippy
Your task is to complete the code below by using the variable name along with the appropriate Scanner and PrintWritermethods.
String name;
Scanner inputFile = new Scanner(new FileReader("dogNames.txt"));
PrintWriter outputFile = new PrintWriter("doggoneData.txt");
.
.
inputFile.close();
outputFile.close();
.
.
Explanation / Answer
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileCopy {
public static void main(String[] args) throws FileNotFoundException{
//open reader file
Scanner inputFile = new Scanner(new FileReader("dogNames.txt"));
//open write file
PrintWriter outputFile = new PrintWriter("doggoneData.txt");
//gets all the data and writes to ourput file till the end
while(inputFile.hasNext())
{
String rows = inputFile.next();
outputFile.write(rows+" "); // ' ' to distinguish every line
}
//close file pointers
inputFile.close();
outputFile.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.