import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * IO operation * 1. Pay attention to appending and overwriting * 2. Streams can only operate on files * 3. Stream closing rules: first open and then close * 4.read(): Returns the length of bytes currently read * 5. Common grammar * File file = new File(String path); * File file = new File(String parent, String child); * File file = new File(File parent, String child); * InputStream = new FileInputStream(String path); * InputStream = new FileInputStream(String path, boolean flag); * InputStream = new FileInputStream(File file); * InputStream = new FileInputStream(File file, boolean flag); * @author pengjunzhao * @date December 25, 2017 */ public class MyIO { public static void main(String[] args) throws IOException { //Folder copy (recursive) directoryCopy("G:/other/2.jpg", "G:/download"); } /** * Folder copy * @param srcPath source folder path * @param destPath destination folder path * @throws IOException */ public static void directoryCopy(String srcPath, String destPath) throws IOException{ File src = new File(srcPath); File dest = new File(destPath); //1. Whether the target folder exists and is a directory, if not, create it if(!dest.exists() && dest.isDirectory()){ dest.mkdirs (); }else if(dest.isFile()){ System.out.println("The destination folder path cannot be a file type"); return; } // start copying directoryCopyDetail(src, dest); } //Folder copy details public static void directoryCopyDetail(File src, File dest) throws IOException{ //2. The source target is a folder and the folder is created if it does not exist in the sub-directory of the target file File newFile = new File(dest, src.getName()); if(src.isDirectory()){ newFile.mkdirs(); //Create a new directory in the target folder for(File sub:src.listFiles()){ //3. Traverse the folder, copy the file if it is, and call this method repeatedly if it is the directory directoryCopyDetail(sub, newFile); } }else if(src.isFile()){ fileCopy(src, newFile); //Create a file in the target folder } } /** * file copy * @param srcPath * @param destPath * @throws IOException */ public static void fileCopy(File srcPath, File destPath) throws IOException{ if(!srcPath.isFile()){ System.out.println("The source path is not a file type"); return; } /* * There is no need to judge destPath here * Because: When the program outputs to a file, the file is created directly if and only if the file does not exist. An error will be reported when the parent directory of the file does not exist */ //4. Stream implements file content copying InputStream is = new FileInputStream(srcPath); //input stream, write program from file OutputStream os = new FileOutputStream(destPath, false); //Output stream, output from program to file (hard disk). true means append write operation (default overwrite) byte[] bt = new byte[1024]; int len = 0; // current number of bytes read // read sequentially while(-1 != (len=is.read(bt, 0 ,1024))){ // write sequentially os.write (bt, 0, len); } os.flush(); os.close(); is.close(); } }
IO stream operation 1: copy the source file/source folder to the specified directory
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325553274&siteId=291194637
Recommended
Ranking