JAVA
Java | IO| Copy a File |example
Copy files from one directory to another In Java
Copying files from one place to another
//JAVA IO FILE COPY FILE EXAMPLES FROM A SOURCE FOLDER TO DESTINATION FOLDER import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; import java.util.TreeSet; public class CopyFiles { final static String sourceFolders = "C:\\IFORERUNNER"; final static String destFolder = "C:\\DESTFILE"; public static void main(String[] args) { for (int j = 1; j < 11; j++) { String sourceFolder = sourceFolders + j + "\\"; System.out.println(sourceFolder); File allFiles = new File(sourceFolder); String[] fileNames = allFiles.list(); System.out.println(fileNames.length); for (int i = 0; i < fileNames.length; i++) { System.out.println(i + "---------------------"); String fileName = fileNames[i]; String sourceFileName = sourceFolder + fileName; String destFileName = destFolder + fileName; System.out.println(destFileName); File sourFile = new File(sourceFileName); File destFile = new File(destFileName); boolean istransferred = sourFile.renameTo(destFile); System.out.println(istransferred); System.out.println(i + "---------------------"); } } } private static void copyFiles(String sourceFileName, String destFileName) { System.out.println(sourceFileName); try { // Create channel on the source FileChannel srcChannel = new FileInputStream(sourceFileName) .getChannel(); // Create channel on the destination FileChannel dstChannel = new FileOutputStream(destFileName) .getChannel(); // Copy file contents from source to destination dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); // Close the channels srcChannel.close(); dstChannel.close(); } catch (IOException e) { } } }
Please send comments to
vgdarur.javafive@blogger.com
Copyright © 2008 - iForeRunner.com