IO copies the multi-level directory console input file directory and then copies the java files in the directory to D: and counts the number of java

  1. package cn.itcast_05;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9.   
  10. /* 
  11.  * Requirement: Copy the multi-pole folder 
  12.  *  
  13.  * Data source: G:\Java\demos 
  14.  * Destination: G:\\ 
  15.  *  
  16.  * analyze: 
  17.  * A: Encapsulate data source File 
  18.  * B: Encapsulation destination File 
  19.  * C: Determine whether the File is a file or a folder 
  20.  * a: is the folder 
  21.  * Create the folder just under the destination directory 
  22.  * Get all files or folder File objects under the File object 
  23.  * Traverse to get each File object 
  24.  * back to C 
  25.  * b: is the file 
  26.  * just copy (byte stream)     
  27.  */  
  28. public class copyFolderDemo {  
  29.     public static void main(String[] args) throws IOException {  
  30.         // encapsulate the data source  
  31.         File srcFolder = new File("D:\\a\\demos");  
  32.         // encapsulate the destination  
  33.         File destFolder = new File("D:\\");  
  34.   
  35.         // copy folder function  
  36.         copyFolder(srcFolder, destFolder);  
  37.     }  
  38.   
  39.     /** 
  40.      * Copy folder method 
  41.      *  
  42.      * @param srcFolder 
  43.      * data source 
  44.      * @param destFolder 
  45.      * destination 
  46.      */  
  47.     public static void copyFolder(File srcFolder, File destFolder)  
  48.             throws IOException {  
  49.         // Determine if the File is a file or a folder  
  50.         if (srcFolder.isDirectory()) {  
  51.             // folder  
  52.             File newFolder = new File(destFolder, srcFolder.getName());  
  53.             newFolder.mkdir();  
  54.   
  55.             // Get all files or folder File objects under the File object  
  56.             File[] fileArray = srcFolder.listFiles();  
  57.             // Traverse to get each File object  
  58.             for (File file : fileArray) {  
  59.                 copyFolder(file, newFolder);  
  60.             }  
  61.   
  62.         } else {  
  63.             // document  
  64.             File newFile = new File(destFolder, srcFolder.getName());// G:\d.txt  
  65.   
  66.             // copy file function  
  67.             copyFile(srcFolder, newFile);  
  68.         }  
  69.     }  
  70.   
  71.     /** 
  72.      * Copy file method 
  73.      *  
  74.      * @param srcFolder 
  75.      * data source 
  76.      * @param newFile 
  77.      * destination 
  78.      */  
  79.     public static void copyFile(File srcFolder, File newFile)  
  80.             throws IOException {  
  81.         // encapsulate the data source  
  82.         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(  
  83.                 srcFolder));  
  84.         // encapsulate the destination  
  85.         BufferedOutputStream bos = new BufferedOutputStream(  
  86.                 new FileOutputStream(newFile));  
  87.   
  88.         // read and write data  
  89.         byte[] bys = new byte[1024];  
  90.         int len ​​=  0;  
  91.         while ((len = bis.read(bys)) != -1) {  
  92.             bos.write(bys, 0, len);  
  93.             bos.flush();  
  94.         }  
  95.   
  96.         // release resources  
  97.         bos.close();  
  98.         bis.close();  
  99.     }  
  100. }  

 

 

 

 

 

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;
}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324988541&siteId=291194637
Recommended