- package cn.itcast_05;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- /*
- * Requirement: Copy the multi-pole folder
- *
- * Data source: G:\Java\demos
- * Destination: G:\\
- *
- * analyze:
- * A: Encapsulate data source File
- * B: Encapsulation destination File
- * C: Determine whether the File is a file or a folder
- * a: is the folder
- * Create the folder just under the destination directory
- * Get all files or folder File objects under the File object
- * Traverse to get each File object
- * back to C
- * b: is the file
- * just copy (byte stream)
- */
- public class copyFolderDemo {
- public static void main(String[] args) throws IOException {
- // encapsulate the data source
- File srcFolder = new File("D:\\a\\demos");
- // encapsulate the destination
- File destFolder = new File("D:\\");
- // copy folder function
- copyFolder(srcFolder, destFolder);
- }
- /**
- * Copy folder method
- *
- * @param srcFolder
- * data source
- * @param destFolder
- * destination
- */
- public static void copyFolder(File srcFolder, File destFolder)
- throws IOException {
- // Determine if the File is a file or a folder
- if (srcFolder.isDirectory()) {
- // folder
- File newFolder = new File(destFolder, srcFolder.getName());
- newFolder.mkdir();
- // Get all files or folder File objects under the File object
- File[] fileArray = srcFolder.listFiles();
- // Traverse to get each File object
- for (File file : fileArray) {
- copyFolder(file, newFolder);
- }
- } else {
- // document
- File newFile = new File(destFolder, srcFolder.getName());// G:\d.txt
- // copy file function
- copyFile(srcFolder, newFile);
- }
- }
- /**
- * Copy file method
- *
- * @param srcFolder
- * data source
- * @param newFile
- * destination
- */
- public static void copyFile(File srcFolder, File newFile)
- throws IOException {
- // encapsulate the data source
- BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
- srcFolder));
- // encapsulate the destination
- BufferedOutputStream bos = new BufferedOutputStream(
- new FileOutputStream(newFile));
- // read and write data
- byte[] bys = new byte[1024];
- int len = 0;
- while ((len = bis.read(bys)) != -1) {
- bos.write(bys, 0, len);
- bos.flush();
- }
- // release resources
- bos.close();
- bis.close();
- }
- }
Get the input file directory from the console, then copy the .java files in the directory ( including subdirectories ) to the D:/java folder , and count the number of java files .
Tip : If there are files with the same name , if there are two Test01.java, there can only be one Test01.java when copying to the target folder , and the other Test01.java should be changed to another name : the name can be randomly generated , Just don't repeat it .
package cn.itcast.demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Test02 {
static int count = 0;
public static void main(String[] args) throws IOException {
// get the source folder
File srcDir = inputFile();
// create the target folder object
File destDir = new File("d:/java");
destDir.mkdirs ();
// copy Java file
copyJavaFile(srcDir, destDir);
System.out.println("count = " + count);
}
/*
* Copy all the .Java files in the srcDir folder to the destDir folder
*/
public static void copyJavaFile(File srcDir, File destDir)
throws IOException {
// Get all the .java files and subfolders in the source folder
File[] files = srcDir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()
|| pathname.getName().endsWith(".java"))
return true;
return false;
}
});
// Traverse the file array
for (File file : files) {
// Determine if file is a folder
if (file.isDirectory()) {
// Recursively call the current method
copyJavaFile(file, destDir);
} else { // If it is a file, copy it to the destination folder destDir
// Get the file name to be copied
String fileName = file.getName();
// Create a file with the same name under the destination folder destDir
File destFile = new File(destDir, fileName);
// Determine whether the destination file exists
while (destFile.exists()) {
/ / Rename the destination file name
destFile = new File(destDir, new Random().nextInt(100000)
+ fileName);
}
System.out.println(destFile);
copyFile(file, destFile);
}
}
}
/*
* Byte stream buffer stream read and write byte array
*/
private static void copyFile(File srcFile, File destFile)
throws IOException {
// The counter is incremented by one
count++;
// Create byte buffer input stream
BufferedInputStream bis = new BufferedInputStream (new FileInputStream(
srcFile));
// create a byte buffered output stream
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(destFile));
int len = 0;
byte[] buffer = new byte[1024];
// loop to read data
while ((len = bis.read(buffer)) != -1) {
// use output stream to write data to output destination
bos.write(buffer, 0, len);
}
// close the resource
bis.close();
bos.close();
}
/*
* Create a file object based on the path string entered by the user
*/
private static File inputFile() {
// Create a keyboard input object
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a file Folder path: ");
// Receive the path entered by the user
String filePath = sc.nextLine();
// Create a file object according to the path string
File dir = new File(filePath);
if (!dir.exists())
throw new RuntimeException("The file path you entered does not exist!!!");
// Determine if it is a folder
if (!dir.isDirectory()) {
throw new RuntimeException("The entered path is not a folder path!!!" );
}
return dir;
}
}