Post Reply 
 
Thread Rating:
  • 1 Votes - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Moving files from source folder to destination folder using Java
02-06-2011, 08:48 AM
Post: #1
Moving files from source folder to destination folder using Java
Java code to copy or move the files from the source folder to destination folder using Java IO API .

Find below the example code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestCode {

public static void main(String[] args) throws Exception {
File source = new File("C:\\source\\Response.xml");
File dest = new File("C:\\dest\\Response.xml");

System.out.println(moveFileToDestLoc(source, dest));
}

public static boolean moveFileToDestLoc(File sourceFile, File destFile)
throws IOException {
System.out.println("TestCode---MoveFile Started");
boolean flag = true;
FileInputStream from = null;
FileOutputStream to = null;
try {
from = new FileInputStream(sourceFile);
to = new FileOutputStream(destFile);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = from.read(buffer)) != -1)
to.write(buffer, 0, bytesRead); // write
} catch (Exception e) {
System.out
.println("Exception While moving file to Destination" + e);
flag = false;
e.printStackTrace();
} finally {
if (from != null)
try {
from.close();
} catch (IOException e) {
;
}
if (to != null)
try {
to.close();
} catch (IOException e) {
;
}
}
System.out.println("TestCode---MoveFile Sucessful");
return flag;

}
}
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:



Send your comments, Suggestions or Queries regarding this site at info@tutorials4u.net

Copyright © 2010 Tutorials4u.net All Rights Reserved