You are given a String variable named file. It contains the name of a one-line f
ID: 3658583 • Letter: Y
Question
You are given a String variable named file. It contains the name of a one-line file. Write a sequence of statements that make an exact copy of the file and give it the same name but with the string ".cpy" attached to the filename. Make sure that the data written to the file has been flushed from its buffer and that any system resources used during the course of running these statements have been released.(Do not concern yourself with any possible exceptions here-- assume they are handled elsewhere.) my code is Scanner input = new Scanner(file); String newFileName = file + ".cpy"; PrintWriter output = new PrintWriter(new File(newFileName)); output.print(input.nextLine()); input.close(); output.close(); I don't know what's wrong here, please help me!Thanks!!Explanation / Answer
String file = "haha"; Scanner scan = new Scanner(new FileReader(file)); String newFileName = file + ".cpy"; FileWriter cpyFile = new FileWriter(newFileName); while(scan.hasNext()) { cpyFile.write(scan.next()); } scan.close(); cpyFile.close();
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.