Sample code is below. It will copy the target files and directory from one location to another. What's considered best practice for handling IO Exceptions while coping files across a network? I used printStackTrace() but feel like this is just a place holder for a better solution. Is logging the answer and should there be another step beyond logging to actually "handle" an error? Thank you for you feedback.
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** This is a test program to copy a directory(s) & file(s) from one location to another. */ public class CopyTest < public static void main(String[] args) < //Declarations String sourcePath = "I:\\MB\\PO"; String destPath = "C:\\testPO\\"; System.out.println("Source path: " + sourcePath); System.out.println("Destination path: " + destPath); File source = new File(sourcePath); File dest = new File(destPath); //Process //Call to method copyUsingStream long start = System.nanoTime(); //start recording how much time the copy takes. copyUsingStream(source, dest); //method to copy the directory/files. System.out.println("Time taken to copy the file: "+(System.nanoTime() -start) + " nanoseconds"); >//end main method /** The copyUsingStream method is a recursive method to copy folders and files from one location to another. */ private static void copyUsingStream(File source, File dest) < if (!source.isDirectory())< // If source is a file ->copy it to the new folder InputStream inStream = null; OutputStream outStream = null; try < inStream = new FileInputStream(source); outStream = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; while ((length = inStream.read(buffer)) >0) < outStream.write(buffer, 0, length); >> catch(IOException ioe) < ioe.printStackTrace(); >finally < try< inStream.close(); outStream.close(); System.out.println("File copied from " + source + " to " + dest + "successfully"); >catch(IOException ioe2) < ioe2.printStackTrace(); >> > else < //If a directory ->create the directory inside the new destination //List all contents if (!dest.exists()) < dest.mkdir(); System.out.println("Directory copied from " + source + " to " + dest + "successfully"); >String folder_contents[] = source.list(); for (String file : folder_contents) < File srcFile = new File(source, file); File destFile = new File(dest, file); copyUsingStream(srcFile, destFile); >> > //end method copyUsingStream > //end class CopyTest
Method without the catches:
private static void copyUsingStream(File source, File dest) throws IOException < if (!source.isDirectory())< // If source is a file ->copy it to the new folder InputStream inStream = null; OutputStream outStream = null; try < inStream = new FileInputStream(source); outStream = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; while ((length = inStream.read(buffer)) >0) < outStream.write(buffer, 0, length); >> finally < inStream.close(); outStream.close(); System.out.println("File copied from " + source + " to " + dest + "successfully"); >> else < //If a directory ->create the directory inside the new destination //List all contents if (!dest.exists()) < dest.mkdir(); System.out.println("Directory copied from " + source + " to " + dest + "successfully"); >String folder_contents[] = source.list(); for (String file : folder_contents) < File srcFile = new File(source, file); File destFile = new File(dest, file); copyUsingStream(srcFile, destFile); >> > //end method copyUsingStream