Java Based Programming. The CopyFile Class Create an application class called Co
ID: 3919325 • Letter: J
Question
Java Based Programming. The CopyFile Class Create an application class called CopyFile with the following fields and methods:
Fields:
• private static Scanner input
• private static Formatter output
Methods:
• public static void openInFile(String fileName) - Gets a file-name and creates the input object according to the file-name.
• public static void openOutFile(String fileName) - Gets a file-name and creates the output object according to the file-name.
• public static void copyRecords() - Reads records from the input file and writes them into the output file. (After writing each record into the output file, shows that record on the screen) - Shows a successful message after copying all records.
• public static void closeInFile() - Closes the input object
• public static void closeOutFile() - Closes the output object
• public static void main(String[] args) - Calls openInFile method to open "Student.txt" file as the source file. - Calls openOutFile method to open "StudentCopy.txt" file as the destination file. - Calls copyRecords method to copy records from "Student.txt" file into "StudentCopy.txt" file. - Calls closeInFile method to close the input file (source file). - Calls closeOutFile method to close the output file (destination file).
Note: Student.txt file is attached as the source file for copying
Student.txt
Explanation / Answer
Student.txt
1111 Sophia Miller Finance 3.24
1122 Mason Brown Marketing 3.05
1133 William Johnson Economics 3.71
1144 Emma Wilson Accounting 4.00
1155 Abigail Jones Marketing 3.52
1166 Emily Davis Management 2.98
1177 James Anderson Finance 3.36
1188 Madison Taylor Management 3.82
1199 Robert Smith Accounting 2.91
1211 David Williams Economics 3.60
___________
CopyFile.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Formatter;
import java.util.Scanner;
public class CopyFile {
private static Scanner sc=null;
private static Formatter f=null;
public static void openInFile(String fileName) throws FileNotFoundException
{
sc=new Scanner(new File(fileName));
}
public static void openOutFile(String fileName) throws IOException
{
f= new Formatter(fileName);
}
public static void copyRecords()
{
while(sc.hasNext())
{
f.format("%s ",sc.nextLine());
}
}
public static void closeInFile()
{
sc.close();
}
public static void closeOutFile()
{
f.close();
}
public static void main(String[] args) {
try {
openInFile("Student.txt");
openOutFile("StudentCopy.txt");
copyRecords();
closeInFile();
closeOutFile();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
______________
Output File:
StudentCopy.txt
1111 Sophia Miller Finance 3.24
1122 Mason Brown Marketing 3.05
1133 William Johnson Economics 3.71
1144 Emma Wilson Accounting 4.00
1155 Abigail Jones Marketing 3.52
1166 Emily Davis Management 2.98
1177 James Anderson Finance 3.36
1188 Madison Taylor Management 3.82
1199 Robert Smith Accounting 2.91
1211 David Williams Economics 3.60
_________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.