Complete the Java program below so that it reads the text file dogNames.txt and
ID: 3685240 • 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
Hello there,
Kindly find below code and input/output files..
package com.dipal.huffman;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* It reads the text file and writes its content to another file.
*
* @author dipal.prajapati
*
*/
public class FileReadingApp {
public static void main(String args[]) {
Scanner inputFile = null;
PrintWriter outputFile = null;
try {
// Creating Scanner instance to read File in Java
inputFile = new Scanner(new FileReader("dogNames.txt"));
outputFile = new PrintWriter("doggoneData.txt");
// Reading each line of file using Scanner class
int lineNumber = 1;
while (inputFile.hasNextLine()) {
String line = inputFile.nextLine();
System.out.println("Written line " + lineNumber + " :" + line);
outputFile.write(line + " ");
lineNumber++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
// close
inputFile.close();
outputFile.close();
}
}
}
====
INPUT file :dogName.txt
Tippy
Johny
OUTPUT file:doggoneData.txt
Tippy
Johny
Let me know if you have any queries.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.