Here is the example of copying file in java. This example will copy file and its content from one file to another.
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class CopyFileExample { public static void main(String[] args) { InputStream inStream = null; OutputStream outStream = null; try { File fileOne =new File("D:\\db\\fileTest\\file2.txt"); File fileTwo =new File("D:\\db\\fileTest\\file3.txt"); inStream = new FileInputStream(fileOne); outStream = new FileOutputStream(fileTwo); byte[] buffer = new byte[1024]; int length; //start copy file content in bytes while ((length = inStream.read(buffer)) > 0) { outStream.write(buffer, 0, length); } inStream.close(); outStream.close(); System.out.println("File copied successfully"); } catch(IOException e) { e.printStackTrace(); } } }
No comments:
Post a Comment