Preface: IO streams are mainly divided into two categories, namely byte streams and character streams
Notice:
1. Use byte streams for audio files, pictures, and videos (wide range).
2. If it only involves text, use character streams
Copy text content using byte stream (other files can also be)
The code is as follows:
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;public class CopyFile { public static void main(String[] args) { try{ File inFile=new File("src.txt"); //Source file InputStream in=new FileInputStream(inFile); //File input stream File outFile=new File("tar.txt"); //Target file OutputStream out=new FileOutputStream(outFile); //File output stream byte[] buff=new byte[1024]; //Create a buffer and allocate 1024 bytes of memory to buff int num=0; while((num=in.read(buff))!=-1){ //Judge whether the maximum number of bytes read is 1024 out.write(buff,0,num); // out.write(buff); //This is OK, but it is easy to cause incorrect file content out.flush(); //Fresh the buffer String s=new String(buff); System.out.println("File content copied successfully"); System.out.println("File copy content is /n"+s); } in.close(); //Close the input stream out.close(); //Close the output stream// inFile.delete(); //Delete the source file// outFile.renameTo(new File("reName.txt")); //Rename the output file} catch(FileNotFoundException e){ e.printStackTrace(); //Print the exception information on the command line and the reason for the error in the program} catch(IOException e){ e.printStackTrace(); } }}Copy text content using character streams (text files only)
The code is as follows:
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class CopyFile{ public static void main(String[] args) { try{ File inFile=new File("D:/src.txt"); //Source file FileReader fileReader=new FileReader(inFile); //Read file character stream BufferedReader bufferedReader=new BufferedReader(fileReader); //Put the read characters into the cache File outFile=new File("D:/tar.txt"); //Target file FileWriter fileWrite=new FileWriter(outFile); //Write character stream BufferedWriter bufferedWriter=new BufferedWriter(fileWrite); //Put the character stream to be entered into the cache String s=""; String str = ""; while((s=bufferedReader.readLine())!=null){ //Determine whether the file character stream has been read bufferedWriter.write(s); //Character stream cache is written to the file bufferedWriter.newLine(); //Read by line, if newline character is encountered, newline bufferedWriter.flush(); //Fresh cache str+=s; } System.out.println("File copy content is /n"+str); //Close the input and output stream fileReader.close();bufferedReader.close(); fileWrite.close();bufferedWriter.close();bufferedWriter.close();//inFile.delete(); //Delete the source file// outFile.renameTo(new File("reName.txt")); //Rename the output file} catch(FileNotFoundException e){ e.printStackTrace(); //Print the exception information on the command line and the reason for the error in the program} catch(IOException e){ e.printStackTrace(); } }}illustrate:
If you want to copy text content by appending file content instead of overwriting it, modify it as follows
OutputStream out=new FileOutputStream(outFile,true); //Byte Stream
FileWriter fileWrite=new FileWriter(outFile,true); //Character stream
If you want to specify the cache size in the character stream, modify it as follows
BufferedWriter bufferedWriter=new BufferedWriter(fileWrite,1024);