Saturday, July 7, 2018

Solution TWO for 12 to 2 batch : 06 - JULY - 2018

import java.util.Scanner;
import java.io.*;
public class FileClassAssignmentTwo 
{
public static void main(String[] args) 
{
try 
{
// create object of scanner class to take input from keyboard
Scanner sc = new Scanner(System.in);

System.out.print("Enter the path for source file: ");
String source = sc.nextLine();

System.out.print("Enter the path for destination file: ");
String destination = sc.nextLine();

// open the source file in read mode
FileInputStream fi = new FileInputStream(source);

// show a menu
System.out.println("# Menu #");
System.out.println("* to copy data of source in destination enter 1 *");
System.out.println("* to join data of source in destination enter 2 *");
System.out.println("* for invalid choice the file will be open in write mode *");


// take int value from keyboard and store inside variable choice
int choice = sc.nextInt();

               // this variable will be used as an indicator to show if destination file will
               // be open in write mode or append mode
               // by default we are assuming that file be open in write  mode 
      boolean flag = false;

// variables to store size of destination and source
long sizeOfDestination = 0, sizeOfSource = 0;


// check if choice is 1
if(choice == 1)
{
flag = false;
}
else 
if(choice == 2) // check if choice is 2
{
flag = true;

// denote the destination path using file class
File file = new File(destination);
 
// check if destination file is already present
if(file.exists())
{
// get the size of destination file
sizeOfDestination = file.length();
}
else // means destination file is not present
{
// set the destination file size to zero
sizeOfDestination = 0;
}

 // end of if of choice 2
// open destination file in write mode or append mode
// as per user choice
// if flag is true it is append mode otherwise it is
// write mode
FileOutputStream fo = new FileOutputStream(destination, flag);
 
// get no of bytes available to read inside source file
int bytes = fi.available();
 
// create an array of byte to store data of file
// size of this array should be equal to the bytes 
byte[] fileData = new byte[bytes];

// fetch data from file and store that data inside array of byte
fi.read(fileData);
 
// store the data of array of byte inside the destination file
fo.write(fileData);
 
// show a message
// to show a double quotes it is mandatory to apply \
// before double quotes like \" means "
System.out.println("file has been created in \""+destination+"\" location");

if(sizeOfDestination > 0)
{
System.out.println("** Previous size of destination was "+sizeOfDestinatio
                     +" bytes **");
System.out.println("** New size of destination is "+(sizeOfDestination 
                     + bytes)+" bytes **");
}
else
{
System.out.println("Size of destination is "+bytes+" bytes");
}

} // end of try block
catch (Exception e) 
{
System.out.println("error " + e);
}

} // end of main method

} // end of class

1 comment: