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

Assume the availability of a public static method named s2f that takes two Strin

ID: 3695571 • Letter: A

Question

Assume the availability of a public static method named s2f that takes two String arguments , the name of a file and some text. The method creates the file and writes the text to it. If the method is successful in doing this it returns true , otherwise false . Assume also the availability of a public static method named f2s that takes a single String argument , the name of a file. The method returns the contents of the file as a String . If the file cannot be opened the method returns null. Given these two methods , which are defined in the class FileUtils, write a method f2f that receives two String arguments , each a file name . The method copies the contents of the file associated with the first argument into the file associated with the second argument . In this exercise you should only use the two available methods s2f and f2s: you should not directly involve the Java IO classes. Your method should return true if successful, false otherwise.

Explanation / Answer

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

/**
* @author srinu
*
*/
public class FileUtils {

   /**
   * method creates the file and writes the text to it. If the method is
   * successful in doing this it returns true , otherwise false
   *
   * @param fileName
   * @param text
   * @return
   */
   public static boolean s2f(String fileName, String text) {

       try {

           File file = new File(fileName);

           // if file doesnt exists, then create it
           if (!file.exists()) {
               file.createNewFile();
           }

           FileWriter fw = new FileWriter(file.getAbsoluteFile());
           BufferedWriter bw = new BufferedWriter(fw);
           bw.write(text);
           bw.close();

       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
           return false;
       }

       return true;
   }

   /**
   * method returns the contents of the file as a String . If the file cannot
   * be opened the method returns null
   *
   * @param fileName
   * @return
   */
   public static String f2s(String fileName) {

       String text = "";
       Scanner scanner = null;
       try {
           File file = new File(fileName);
           // if file doesnt exists, then create it
           if (!file.exists()) {
               return null;
           }
           scanner = new Scanner(file);
           while (scanner.hasNext())
               text += scanner.nextLine() + " ";

           scanner.close();
           return text;

       } catch (Exception e) {
           // TODO: handle exception
           e.printStackTrace();
           return null;
       }

   }

   /**
   * method copies the contents of the file associated with the first argument
   * into the file associated with the second argument return true if
   * successful, false otherwise.
   *
   * @param sourceFileName
   * @param destinationFileName
   * @return
   */
   public static boolean f2f(String sourceFileName, String destinationFileName) {

       if (s2f(destinationFileName, f2s(sourceFileName))) {
           return true;

       } else {
           return false;

       }

   }

   /**
   * @param args
   */
   public static void main(String[] args) {

       String sourceFileName = "sample.txt", destinationFileName = "sample1.txt";

       // write some data to file
       s2f(sourceFileName, "Hai this is srinivas");

       // copy content from one file to another file(sourceFileName->
       // destinationFileName)
       if (f2f(sourceFileName, destinationFileName)) {
           System.out.println("File copied Successfully");

       } else {
           System.out.println("File is not copied");
       }
   }
}

OUTPUT:

File copied Successfully

sample.txt

Hai this is srinivas

sample1.txt

Hai this is srinivas

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