Showing posts with label File. Show all posts
Showing posts with label File. Show all posts

File Copy in Java with Example, Code to copy contents of one file to another file.

/*The following program accepts two files from the command line and copies one file into another file.
For copying one file to another file , we may check the following conditions.

1) Source file exists or not
2) It is a file or not
3) The file is readble or not (using canRead()))
4) Target file already exists or not . If already exists , prompt to overwrite.
5) Destination folder exist or not
6) Target folder is writeable or not
For checking the above conditions, we can use File class in java.io.


For Copying the file ,
-> We have to create InputStream and OutputStream
-> Read the bytes from the source file until EOF
-> Write the bytes into the target file.



I have used 2 methods to copy the files. In the method 2 , the whole file contents are read at once and copied to the target file.
*/



import java.io.*;
import java.util.*;

public class FileCopy
{
public static void main(String [] args)
{


if(args.length!=2) { System.out.println("Improper arguments . Syntax : java filecopy sourcefile targetfile "); System.exit(0); }

File source_file=new File(args[0]);
File target_file=new File(args[1]);

// First make sure the source file exists, is a file, and is readable.
if (!source_file.exists())
{

System.out.println("Source File Not Found");
System.exit(0);
}

if (!source_file.isFile())
{

System.out.println(source_file + " is Not a file");
System.exit(0);
}


if (target_file.isDirectory())
target_file = new File(target_file, source_file.getName());

String YN="Y";
if (target_file.exists()) {
System.out.print("Target file already exists . Do you overwrite ? (y/n) ");
System.out.flush();
Scanner scanner=new Scanner(System.in);
YN=scanner.next();
}
//BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
// String response = in.readLine();

// Check the response. If not a Yes, abort the copy.
if (YN.equalsIgnoreCase ("Y") )
{
String target_dir = target_file.getParent();

if (target_dir == null) target_dir = System.getProperty("user.dir");
File folder = new File(target_dir);
if (!folder.exists())
{
System.out.println("Destination Folder doesn't exist " + target_dir);
System.exit(0);
}

if (folder.isFile())
{
System.out.println("Destination is not a directory: " + target_dir);
System.exit(0);
}

}
else
{
System.out.println("Abort Overwriting" ) ;
System.exit(0);
}

try
{

copyFile(source_file, target_file);
// copy(source_file, target_file); // for using the method 1

}
catch (Exception e)
{
System.out.println("Error");
}



}


//Method 1


private static void copy(File source, File target) throws IOException {

System.out.println("Copying File......");

InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(target);

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) !=-1) {
out.write(buf, 0, len);
}
in.close();
out.close();

System.out.println(" file copied");
}


// Method 2

private static void copyFile(File source, File target) throws IOException {

System.out.println("Copying File......");

InputStream in = new FileInputStream(source);
OutputStream out = new FileOutputStream(target);

// Transfer bytes from in to out
byte[] buf = new byte[(int) source.length()];
in.read(buf);

out.write(buf);

in.close();
out.close();

System.out.println(" file copied");
}

}


Running the above program

D:\as2\j2eeforum>java FileCopy FileCopy.java FileCopy2.java
Copying File......
file copied

D:\as2\j2eeforum>java FileCopy d:\as2\kk.txt kk1.txt
Target file already exists . Do you overwrite ? (y/n) y
Copying File......
file copied


D:\as2\j2eeforum>java FileCopy d:\as2\kk.txt kk1.txt
Target file already exists . Do you overwrite ? (y/n) n
Abort Overwriting

D:\as2\j2eeforum>java FileCopy d:\as2\kkk.txt kk1.txt
Source File Not Found
Read more...

 
Copyright 2011 Java Online