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

Develop a class BackupRestore that has two static methods: public static int bac

ID: 3799980 • Letter: D

Question

Develop a class BackupRestore that has two static methods:

public static int backup(String filename, double partSize)

public static int restore(String filename, int numberOfPieces)

The backup method backs up huge files by splitting them into smaller pieces that may fit into storage devices with different capacities (e.g., 700 mb for CD-Rs). The size of each piece is equal to partSize megabytes. The method creates the output files filename.1, filename.2, etc. and then returns an integer representing the number of files created.

The restore methods does the opposite. It combines filename.1, filename.2, etc. into filename. The method returns an integer representing the total size of the file filename. Mos t questions are based on / taken from the exercises and examples listed in the textbook used by the course.

In both methods, if an IOException occurs, display an error message and return -1. Use buffer classes to improve performance.

Try your code on the course syllabus (syllabus.pdf), i.e., split it into pieces of e.g. 0.25 MB size, delete the original file, and then combine your pieces back again and open the file to see if your code works.

Explanation / Answer

//rather than passing i declared as constants if you want delete constants and declare in main method and pass those as parameters


import java.io.*;
import java.util.*;
public class FileOperations {
   private static String FILE_NAME = "D:\puppyprogram java.txt";
   private static byte PART_SIZE = 5;
  
   public static void restore(int noofpieces)
   {
  
           File outputfile = new File(FILE_NAME);
           FileOutputStream fileoutputstream;
           FileInputStream fileinputstream;
           byte[] fileBytes;
           int bytesRead = 0;
           List<File> filelist = new ArrayList<File>();
           for(int i=0;i<noofpieces;i++)
               filelist.add(new File(FILE_NAME+".part"+i));
           try {
               fileoutputstream = new FileOutputStream(outputfile,true);
           for (File f : filelist) {
               fileinputstream = new FileInputStream(f);
           fileBytes = new byte[(int) f.length()];
           bytesRead = fileinputstream.read(fileBytes, 0,(int) f.length());
           assert(bytesRead == fileBytes.length);
           assert(bytesRead == (int) f.length());
           fileoutputstream.write(fileBytes);
           fileoutputstream.flush();
           fileBytes = null;
           fileinputstream.close();
           fileinputstream = null;
           }
           fileoutputstream.close();
           fileoutputstream = null;
           }catch (Exception exception){
               System.out.println("exception");
           }
       }

  
   public static void backup() {
       File inputFile = new File(FILE_NAME);
       FileInputStream fis;
       String newFileName;
       FileOutputStream fos;
       int fileSize = (int) inputFile.length();
       int noofChunks = 0, reading = 0, readingsize = PART_SIZE;
       byte[] bytePart;
       try {
           fis = new FileInputStream(inputFile);
           while (fileSize > 0) {
               if (fileSize <= 5) {
                   readingsize = fileSize;
               }
               bytePart = new byte[readingsize];
               reading = fis.read(bytePart, 0, readingsize);
               fileSize -= reading;
               assert (reading == bytePart.length);
               noofChunks++;
               newFileName = FILE_NAME + ".part"
                       + Integer.toString(noofChunks - 1);
               fos = new FileOutputStream(new File(newFileName));
               fos.write(bytePart);
               fos.flush();
               fos.close();
               bytePart = null;
               fos = null;
           }
           fis.close();
       } catch (Exception exception) {
           System.out.println("exception");
       }
      
      
   }
   public static void main(String[] args) {
       backup();
       restore(10);
   }
}