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

1.3 Given an initialized string variable filename write a sequence of statements

ID: 3831240 • Letter: 1

Question

1.3 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.

EXAMPLE QUESTIONS WITH ANSWERS:

Q. Given an initialized String variable outfile, write a statement that declares a PrintWriter reference variable name output and initializes it to a reference to a newly created PrintWriter object associated witha file whose name is given by outfile. (Do not concern yourslef with any possilbe exeptions here--assume they are handled elsewhere.)

A. PrintWriter output = new PrintWriter(outfile);

Q. Given an initialized String variable fileName, write a sequence of statements that create a file whose name is given by the variable and whose content is a single line consisting of "This Is FIle:" followed by the name of the file. 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)

A. FileWriter fw = new FileWriter(fileName); BufferedWriter bw = new BufferedWriter(fw); bw.write("This Is File: " + fileName); bw.close();

Q. Given a String variable named line1, write a sequence of statements that use a Scanner to read the first line of a file named "poem" and stores it in line1. (Do not concern yourself with any possible exceptions here--assume they are handled elsewhere.)

A. File fileInput = new File("poem"); Scanner scan = new Scanner(fileInput); line1=scan.nextLine();

Explanation / Answer

FileContentTest.java

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


public class FileContentTest {

  
   public static void main(String[] args) throws FileNotFoundException {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter the file name: ");
       String filename = scan.next();
       String data = f2s(filename);
       System.out.println("Enter the file name: ");
       String outputfilename = scan.next();
      
       writeFile(data,outputfilename);
   }
   public static void writeFile(String data, String filename) throws FileNotFoundException{
       PrintWriter pw = new PrintWriter(new File(filename));
       pw.write(data);
       pw.flush();
       pw.close();
       System.out.println("File has been generated");
   }

   public static String f2s(String filename) throws FileNotFoundException{
       File file = new File(filename);
       if(file.exists()){
           Scanner scan = new Scanner(file);
           String s = "";
           while(scan.hasNextLine()){
               s = s + scan.nextLine()+" ";;
           }
           return s;
       }
       else{
           return null;
       }
   }
}

Output:

Enter the file name:
D:\data.txt
Enter the file name:
D:\output.txt
File has been generated

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