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

//Activity 3 // Copy a file using FileInputStreams and FileOutputStreams (20 //

ID: 3827659 • Letter: #

Question

//Activity 3
//       Copy a file using FileInputStreams and FileOutputStreams (20
//               mins)
//               • Use FileInputStreams and FileOutputStreams to copy the “joke” file to
//               a new filed named “copyJoke”
//               • Close the streams when you finish copying
//               • Check if the copied file exist
//               • Answer question 1, Activity 3
//               • Demonstrate   the   result   to   your   TA   and   get   a   signature
//      
       //TODO: this part Copy the content of file name "Joke" and save as "copyOfJoke" in the same folder
       //Dont forget to close the streams when you finish
       //After finish copying, call .exist() method to check if the copied file exist

Explanation / Answer

Hi, Please find my implementation.

Please let me know in case of any issue.

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

public class CopyOfJoke {

   public static void main(String[] args) throws IOException {

       File inFile = new File("joke.txt");

       File outFile = new File("copyJoke.txt");

       FileInputStream fis = new FileInputStream(inFile);

       FileOutputStream fos = new FileOutputStream(outFile);

       int i;

       char c;

       // read till the end of the file

       while((i = fis.read())!=-1) {

           // converts integer to character

           c = (char)i;

           fos.write(c);

       }

      

       fis.close();

       fos.close();

       if (outFile.exists()) {

           System.out.println("copyJoke.txt exist");

       }else{

           System.out.println("copyJoke.txt does not exist");

       }

   }

}