Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given an initialized String variable fileName, write a sequence of statements th

ID: 3818709 • Letter: G

Question

Given an initialized String variable fileName, write a sequence of statements that append the line "All is well." to the file whose name is given by the variable. 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.) 1/*FileWriter fw = new FileWriter("greeting");//created file 2 BufferedWriter bw = new BufferedWriter(fw);//assign writer 3 bw.write("Hello, World!");//write line 4 bw.close();//close the file */5 Printwriter fileName = new Printwriter("All is well."); 6 fileName.flush(); 7 fileName.close();

Explanation / Answer

import java.io.*;
public class HelloWorld{

public static void main(String []args) throws IOException{
String filename="input.txt";
//out = new BufferedWriter(new FileWriter("file1.txt"));
  
BufferedWriter out = new BufferedWriter(new FileWriter(filename,true));//
out.write(" All is well");
out.flush();//flush the buffer
}
}

/*Using printwriter*/

import java.io.*;
public class HelloWorld{

public static void main(String []args) throws IOException{
String filename="input.txt";
PrintWriter out = new PrintWriter(new FileOutputStream(filename,true));
//BufferedWriter out = new BufferedWriter(new FileWriter(filename,true));
out.write("All is well");
out.flush();//flush the buffer
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote