1 File reading, mainly look at the summary of the comment section
/** * file reading * 1. Establish a contact File object 2. Select the stream file input stream InputStream FileInputStream 3. Operation: byte[] car =new byte[1024]; +read+read size The output will print out what is read 4. Release resources: close 5. The File class is equivalent to a pipeline for establishing a program and a physical disk. InputSteam OutputStream is equivalent to an electric pump, which pushes out or pulls in the contents of the file. And in or out is relative to the program, so if the data is from the file to the program, use InputSteam, if the data is from the program to the file, use OutputStream * @author Administrator * */ public class Demo01 { /** * @param args */ public static void main(String[] args) { //1, establish a contact File object File src =new File("e:/xp/test/a.txt"); //2, select stream InputStream is =null; //promote scope try { is =new FileInputStream(src); //3, the operation continuously reads the buffer array byte[] car =new byte[1024]; int len =0; //Receive actual read size //loop read StringBuilder sb =new StringBuilder(); while(-1!=(len=is.read(car))){ // Convert output byte array to string String info =new String(car,0,len); sb.append(info); } System.out.println(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace (); System.out.println("File does not exist"); } catch (IOException e) { e.printStackTrace (); System.out.println("Failed to read file"); }finally{ try { //4, release resources if (null != is) { is.close(); } } catch (Exception e2) { System.out.println("Failed to close file input stream"); } } } }
2 Write content from one file to another:
/** 1. Establish contact with the source destination of the File object 2. Select the stream File input stream InputStream FileInputStream File output stream OutputStream FileOutputStream 3. Operation: Copy byte[] flush =new byte[1024]; int len = 0; while(-1!=(len=inputstream.read(flush))){ output stream.write(flush,0,len) } output stream.flush 4. Release resources: close two streams * @author Administrator * */ public class CopyFileDemo { /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) { String src ="E:/xp/test"; String dest="e:/xp/test/4.jpg"; try { copyFile (src, dest); } catch (FileNotFoundException e) { e.printStackTrace (); System.out.println("File does not exist"); } catch (IOException e) { e.printStackTrace (); System.out.println("Failed to copy file|Failed to close stream"); } } /** * a copy of the file * @param source file path * @param directory file path * @throws FileNotFoundException,IOException * @return */ public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException { //1. Establish contact source (exist and file) + destination (file may not exist) File src =new File(srcPath); File dest =new File(destPath); if(!src.isFile()){ //not a file or null System.out.println("Only copy files"); throw new IOException("Only copy files"); } //2, select stream InputStream is =new FileInputStream(src); OutputStream os =new FileOutputStream(dest); //3, file copy loop + read + write byte[] flush =new byte[1024]; int len = 0; //read while(-1!=(len=is.read(flush))){ //write out os.write(flush, 0, len); // write the actual length of len } os.flush(); //Force flush //Close the stream that is opened first and then closed os.close(); is.close(); } }
3 File copy tool class: Pay attention to the comments in the experience
/** * file operations * 1. File copy * 2. Folder copy refuses to copy to yourself * @author Administrator * */ public class FileUtil { /** * copy folder * @param src source path * @param dest destination path * @throws IOException * @throws FileNotFoundException */ public static void copyDir(String srcPath,String destPath) throws FileNotFoundException, IOException{ //Refuse to copy to yourself if(srcPath.equals(destPath)){ return ; } File src=new File(srcPath); File dest =new File(destPath); copyDir (src, dest); } /** * copy folder * @param src source File object * @param dest target File object * @throws IOException * @throws FileNotFoundException */ public static void copyDir(File src,File dest) throws FileNotFoundException, IOException{ if(src.isDirectory()){ //Folder dest =new File(dest,src.getName()); // If the source is a folder, append the folder name of the source under the target dest if(dest.getAbsolutePath().contains(src.getAbsolutePath())){ System.out.println("The parent directory cannot be copied to the subdirectory");// e:/test/a cannot be copied to e:/test/a/b return; } } if(dest.exists()) { // The target folder already exists. The w folder cannot directly overwrite the file. There can be something in the folder, so it cannot be overwritten directly. System.out.println("The destination folder already exists"); return; } copyDirDetail (src, dest); } /** * Copy folder details * @param src * @param dest */ public static void copyDirDetail(File src,File dest) throws FileNotFoundException,IOException{ if(src.isFile()){ //File try { FileUtil.copyFile (src, dest); } catch (FileNotFoundException e) { //e.printStackTrace(); throw e; } catch (IOException e) { //e.printStackTrace(); throw e; } }else if(src.isDirectory()){ //Folder // Make sure the destination folder exists dest.mkdirs (); //Get the next level directory | file for(File sub:src.listFiles()){ copyDirDetail(sub,new File(dest,sub.getName())); } } } /** * a copy of the file * @param source file path * @param directory file path * @throws FileNotFoundException,IOException * @return */ public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException { //1. Establish contact source (exist and file) + destination (file may not exist) copyFile(new File(srcPath),new File(destPath)); } /** * a copy of the file * @param source file File object * @param directory file File object * @throws FileNotFoundException,IOException * @return */ public static void copyFile(File src,File dest) throws FileNotFoundException,IOException { if(!src.isFile()){ //not a file or null System.out.println("Only copy files"); throw new IOException("Only copy files"); } //dest is an existing folder and cannot be created from a file with the same name as the folder if(dest.isDirectory()){ System.out.println(dest.getAbsolutePath()+"Cannot create a file with the same name in the folder"); throw new IOException(dest.getAbsolutePath()+"Cannot create a file with the same name in the folder"); } //2, select stream InputStream is =new BufferedInputStream(new FileInputStream(src)); OutputStream os =new BufferedOutputStream(new FileOutputStream(dest)); //3, file copy loop + read + write byte[] flush =new byte[1024]; int len = 0; //read while(-1!=(len=is.read(flush))){ //write out os.write(flush, 0, len); } os.flush(); //Force flush //close the stream os.close(); is.close(); } }
FileUtil append method: close stream public method
/** * The utility class closes the stream * Variable parameters: ... only the last position of the formal parameter, the processing method is the same as that of the array */ public static void close(Closeable ... io){ for(Closeable temp:io){ try { if (null != temp) { temp.close(); } } catch (Exception e) { } } } /** * Use generic methods */ public static <T extends Closeable> void closeAll(T ... io){ for(Closeable temp:io){ try { if (null != temp) { temp.close(); } } catch (Exception e) { } } }