Wednesday, December 22, 2010

Java-Moving a file

import java.io.*;

public class MoveFile {
public static void main(String args[]) throws IOException {
switch (args.length) {
case 0:
System.out.println("Error in usage:");
System.out.println("Usage: java MoveFile ");
break;
case 1:
System.out.println("Specify the destination file.\n");
System.out.println("\tUsage: java MoveFile ");
break;
case 2:
File source_file = new File(args[0]);
File dest_file = new File(args[1]);

/********************************************************************
* If the file does not exist, show an error message *
* and stop the program. *
********************************************************************/
if (! source_file.exists()) {
System.out.println(args[0] + " Not Found !!!");
System.exit(0);
}

/********************************************************************
* If the file exists, ask if the file is to be overwritten. *
* (i) If the response is "Y" or "y", then overwrite it by *
* invoking the move method. *
* (ii)If the response is "N" or "n", then cancel move operation. *
********************************************************************/
if (dest_file.exists()) {
System.out.print(args[1] + " file already exists. Overwrite? (y/n): ");
int response = System.in.read();
if (response == 'Y' || response == 'y') { // if you do not want to overwrite the file.
moveFile(source_file, dest_file); // user-defined function
} else {
System.out.println("File move operation aborted !!!");
System.exit(0);
}
} else {
moveFile(source_file, dest_file);
}
}
}
public static void moveFile(File source_file, File dest_file) throws IOException {
System.out.print("Move file? (y/n): ");
System.in.skip(System.in.available());//Emptying the Keyboard Buffer.
int response = System.in.read();
if (response == 'Y' || response == 'y') { // if you want to move the file.
/**********************************************************************
* Creating streams for reading and writing source/destination files. *
**********************************************************************/
String source_file_str = source_file.getAbsolutePath();
String dest_file_str = dest_file.getAbsolutePath();

String source_file_dir =
source_file_str.substring(0,
source_file_str.lastIndexOf('\\'));//truncating the filename
String dest_file_dir =
dest_file_str.substring(0,
dest_file_str.lastIndexOf('\\'));

System.out.println("Source File Abs Path: " +
source_file_str);
System.out.println("Source Dir: " + source_file_dir);
System.out.println("Dest File Abs Path: " + dest_file_str);
System.out.println("Dest Dir: " + dest_file_dir);

if (source_file_dir.equals(dest_file_dir)) {
System.out.println("Renaming file " +
source_file.getName() + " to " +
dest_file.getName() + " !!!");
source_file.renameTo(new File(dest_file.getName()));
} else {
FileInputStream input_file = new FileInputStream(
source_file);
FileOutputStream output_file = new FileOutputStream(
dest_file);
/**********************************************************************
* (i) Finding the size of the source file using available(). *
* (ii) Creating a byte array, the no. of elements of which is equal *
* to the size of source file. *
* (iii)Read data from the source file into the byte array. *
* (iv) Write the byte array to the destination file. *
**********************************************************************/
System.out.println("Moving file " +
source_file.getName() + " to " +
dest_file.getName() + " !!!");
int file_size = input_file.available();
byte input_file_data[] = new byte[file_size];
input_file.read(input_file_data);
output_file.write(input_file_data);

/**********************************************************************
* Close the input/output streams. *
**********************************************************************/
input_file.close();
output_file.close();

source_file.delete();
}

/**********************************************************************
* Delete the source file and return a successful process message. *
**********************************************************************/

System.out.println("File " + source_file.getName() +
" successfully moved to " + dest_file.getName() + " !!!");
} else {// if you do not want to move the file.
System.out.println("File move operation aborted !!!");
System.exit(0);
}
}
}

0 comments:

Post a Comment

Contact Form

Name

Email *

Message *