1. Copy files through byte streams
/** * Copy files through byte streams * @param sourcePath SourcePath * @param targetPath target file path */ public static void copyFileByStream(String sourcePath,String targetPath){ // Source file path File source = new File(sourcePath); //Target file path File target = new File(targetPath); //If the source file does not exist, it cannot be copied if(!()){ return; } //Create if the target file directory does not exist if(!().exists()){ ().mkdirs(); } try { //Implement copy of files InputStream inputStream = new FileInputStream(source); OutputStream outputStream = new FileOutputStream(target); int temp = 0; // 1024 bytes are read each time byte[] data = new byte[1024]; //Save the data read each time into a byte array and return the number of reads while ((temp = (data)) != -1){ //Output array (data,0,temp); } (); (); } catch (IOException e) { (); } }
2. Realize file copying through character streams
Only text files can be copied using character streams
/** * Copying files through character streams * * @param sourcePath SourcePath * @param targetPath target file path */ public static void copyFileByReaderAndWriter(String sourcePath, String targetPath) { // Source file path File source = new File(sourcePath); //Target file path File target = new File(targetPath); //If the source file does not exist, it cannot be copied if (!()) { return; } //Create if the target file directory does not exist if (!().exists()) { ().mkdirs(); } FileReader in = null; FileWriter out = null; try { //Character input stream and character output stream in = new FileReader(source); out = new FileWriter(target); char[] c = new char[1024]; int temp = 0; //Read 1024 characters each time while ((temp = (c)) != -1) { //Output to file (c, 0, temp); } } catch (IOException e) { (); } finally { //Close the flow try { if (in != null) { (); } if (out != null) { (); } } catch (IOException e) { (); } } }
3. Realize file copying through byte buffer stream
/** * Copy files through byte buffered streams * * @param sourcePath SourcePath * @param targetPath target file path */ public static void copyFileByBuffered(String sourcePath, String targetPath){ // Source file path File source = new File(sourcePath); //Target file path File target = new File(targetPath); //If the source file does not exist, it cannot be copied if (!()) { return; } //Create if the target file directory does not exist if (!().exists()) { ().mkdirs(); } InputStream in = null; OutputStream out = null; try { //Byte buffered input stream and byte buffered output stream in = new BufferedInputStream(new FileInputStream(source)); out = new BufferedOutputStream(new FileOutputStream(target)); byte[] b = new byte[1024]; int temp = 0; //A 1024 byte array is read each time while((temp = (b)) != -1){ //Output to file (b,0,temp); } } catch (Exception e) { (); }finally { //Close the flow try { if (in != null) { (); } if (out != null) { (); } } catch (IOException e) { (); } } }
4. Copy files through character buffering streams
Character buffer streams can only read text files
/** * Copy files through character buffering streams * * @param sourcePath SourcePath * @param targetPath target file path */ public static void copyFileByBufferedChar(String sourcePath, String targetPath){ // Source file path File source = new File(sourcePath); //Target file path File target = new File(targetPath); //If the source file does not exist, it cannot be copied if (!()) { return; } //Create if the target file directory does not exist if (!().exists()) { ().mkdirs(); } BufferedReader in = null; BufferedWriter out = null; try { //Character buffer input stream and character buffer output stream in = new BufferedReader(new FileReader(source)); out = new BufferedWriter(new FileWriter(target)); //Read the file (one line is read each time) String temp = null; while((temp = ()) != null){ //Output to file (temp); } } catch (Exception e) { (); }finally { //Close the flow try { if (in != null) { (); } if (out != null) { (); } } catch (IOException e) { (); } } }
5. Copy files through JAVA NIO indirect buffer
/** * Copy files through JAVA NIO indirect buffer * * @param sourcePath SourcePath * @param targetPath target file path */ public static void copyFileByChannel(String sourcePath, String targetPath) { FileChannel outChannel = null; FileChannel inChannel = null; FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(sourcePath); fos = new FileOutputStream(targetPath); //Get the channel inChannel = (); outChannel = (); //Allocate a buffer of specified size ByteBuffer buf = (1024); while ((buf) != -1) { //Convert to read data mode (); //Write to disk (buf); //Clear the buffer (); } } catch (Exception e) { (); } finally { //Close the flow try { if (outChannel != null) { (); } if (inChannel != null) { (); } if (fis != null) { (); } if (fos != null) { (); } } catch (IOException e) { (); } } }
6. Copy files directly through JAVA NIO
/** * Copy files (memory mapped files) via JAVA NIO Direct Buffer * * @param sourcePath SourcePath * @param targetPath target file path */ public static void copyFileByChannelBufferd(String sourcePath, String targetPath) { FileChannel inChannel = null; FileChannel outChannel = null; try { //Get the channel, which means it is readable, it means it is writable, it means it can be created inChannel = ((sourcePath), ); outChannel = ((targetPath), , , ); //Create memory mapped file MappedByteBuffer inMapped = (.READ_ONLY, 0, ()); MappedByteBuffer outMapped = (.READ_WRITE, 0, ()); //Directly operate the memory mapped file byte[] buf = new byte[()]; (buf); (buf); } catch (IOException e) { (); } finally { //Close the flow try { if (outChannel != null) { (); } if (inChannel != null) { (); } } catch (IOException e) { (); } } }
7. Transfer copy files through JAVA NIO channel
Method one
/** * Transfer copy files through JAVA NIO channel * * @param sourcePath SourcePath * @param targetPath target file path */ public static void copyFileByChannelTransfer(String sourcePath, String targetPath) { FileChannel inChannel = null; FileChannel outChannel = null; try { //Get the channel inChannel = ((sourcePath), ); outChannel = ((targetPath),,,); (0,(),outChannel); } catch (IOException e) { (); }finally { //Close the flow try { if (outChannel != null) { (); } if (inChannel != null) { (); } } catch (IOException e) { (); } } }
Method 2
/** * Transfer copy files through JAVA NIO channel * * @param sourcePath SourcePath * @param targetPath target file path */ public static void copyFileByChannelTransfer2(String sourcePath, String targetPath) { FileInputStream fis = null; FileOutputStream fos = null; FileChannel inChannel = null; FileChannel outChannel = null; try { fis = new FileInputStream(sourcePath); fos = new FileOutputStream(targetPath); //Get the channel inChannel = (); outChannel = (); (0,(),outChannel); } catch (IOException e) { (); }finally { //Close the flow try { if (outChannel != null) { (); } if (inChannel != null) { (); } } catch (IOException e) { (); } } }
Example of usage
String source = "e:\\demo\\The Emperor of Heaven.txt"; String target = "e:\\demo\\"; long time1 = (); copyFileByStream(source, target + ""); ("Time-consuming to copy a file through a byte stream:" + (() - time1)); long time2 = (); copyFileByReaderAndWriter(source, target + ""); ("Time-consuming to copy a file through a character stream:" + (() - time2)); long time3 = (); copyFileByBuffered(source, target + ""); ("Time-consuming to copy a file through a byte buffer stream:" + (() - time3)); long time4 = (); copyFileByBufferedChar(source, target + ""); ("Time-consuming to copy a file through a character buffer stream:" + (() - time4)); long time5 = (); copyFileByChannel(source, target + ""); ("Time-consuming to copy files through JAVA NIO channel (non-direct buffer):" + (() - time5)); long time6 = (); copyFileByChannelBufferd(source, target + ""); ("Time-consuming to copy files through JAVA NIO channel (direct buffer):" + (() - time6)); long time7 = (); copyFileByChannelTransfer(source, target + ""); ("It takes time to copy files through JAVA NIO channel transmission:" + (() - time7)); long time8 = (); copyFileByChannelTransfer(source, target + ""); ("It takes time to copy files through JAVA NIO channel transfer 2:" + (() - time8));
Through testing, it was found that using JAVA NIO channel transmission, JAVA NIO channel direct buffer and byte buffer stream copy files is the most efficient
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.