cancel
Showing results for 
Search instead for 
Did you mean: 

COPY FILES FROM ONE FOLDER TO ANOTHER

Former Member
0 Kudos

Hi All,

I want to copy all files from one folder to another folder,after that i want to delete the files of first folder.

These folders are in Server.

Regards

Trilochan

Accepted Solutions (1)

Accepted Solutions (1)

pravesh_verma
Active Contributor
0 Kudos

Hi,

Please try this code:


File file = new File("Path_of_Your_Source_Directory_in_Server");
		String[] fileList = file.list();
		
		int size = fileList.length;
		String fileName = null;
		FileChannel in =  null;
		FileChannel out = null;
		for (int i = 0; i < size; i++) {
			fileName = fileList<i>;
			try {
				in = new FileInputStream(fileName).getChannel();
				out = new FileOutputStream("Path_of_Your_Destination_Folder (like: C:/server/destination_files)"+ "/" +fileName).getChannel();

				in.transferTo(0, in.size(), out);
				 
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			
			}
			catch (IOException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			catch (Exception e3){
				e3.printStackTrace();
			}
			
		}

If this doesnt work for you then try to use these links as well:

1) http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/820ed8b162c475a6/a5f8a828...recursively~directoryjavahow-to&rnum=1&hl=en%23a5f8a82824f6efe6

2)

3) http://www.roseindia.net/java/example/java/io/MovingFile.shtml

I hope this solves your issue. Please revert back in case you need any further help!

Thanks and Regards

Pravesh

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Parmesh,

Thanks For Your Reply.

it helped me lot,now my problem is solved.

File file = new File("C:/outbox");

File fileList[] = file.listFiles();

int size = fileList.length;

String fileName = null;

for (int i = 0; i < size; i++) {

fileName = fileList<i>.getName();

try {

InputStream in = new FileInputStream("C:/outbox/"+fileName);

OutputStream out = new FileOutputStream("C:/inbox/"+fileName);

// Transfer bytes from in to out

byte[] buf = new byte[1024];

int len;

while ((len = in.read(buf)) > 0) {

out.write(buf, 0, len);

}

in.close();

out.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

Regards

Trilochan