Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to transfer a copy file through a JAVA NIO channel

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to transfer copied files through the JAVA NIO channel. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Transfer copy files through JAVA NIO channel

Mode one

/ * transfer copy files through JAVA NIO channel * * @ param sourcePath source file path * @ param targetPath destination file path * / public static void copyFileByChannelTransfer (String sourcePath, String targetPath) {FileChannel inChannel = null; FileChannel outChannel = null; try {/ / get channel inChannel = FileChannel.open (Paths.get (sourcePath), StandardOpenOption.READ) OutChannel = FileChannel.open (Paths.get (targetPath), StandardOpenOption.WRITE,StandardOpenOption.READ,StandardOpenOption.CREATE); inChannel.transferTo (0jinChannel.size (), outChannel);} catch (IOException e) {e.printStackTrace ();} finally {/ / close stream try {if (outChannel! = null) {outChannel.close () } if (inChannel! = null) {inChannel.close ();}} catch (IOException e) {e.printStackTrace ();}}

Mode two

/ * transfer copy files through JAVA NIO channel * * @ param sourcePath source file path * @ param targetPath destination 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 channel inChannel = fis.getChannel (); outChannel = fos.getChannel (); inChannel.transferTo (0recoverinChannel.size (), outChannel);} catch (IOException e) {e.printStackTrace ();} finally {/ / close stream try {if (outChannel! = null) {outChannel.close () } if (inChannel! = null) {inChannel.close ();}} catch (IOException e) {e.printStackTrace ();}}

Use the example

String source = "e:\\ demo\\"; String target = "e:\\ demo\"; long time1 = System.currentTimeMillis (); copyFileByStream (source, target + "1.txt"); System.out.println ("time to copy files via byte stream:" + (System.currentTimeMillis ()-time1)); long time2 = System.currentTimeMillis (); copyFileByReaderAndWriter (source, target + "2.txt") System.out.println ("time to copy a file through a character stream: + (System.currentTimeMillis ()-time2)); long time3 = System.currentTimeMillis (); copyFileByBuffered (source, target +" 3.txt "); System.out.println (" time to copy a file through a byte buffer stream: "+ (System.currentTimeMillis ()-time3)); long time4 = System.currentTimeMillis (); copyFileByBufferedChar (source, target +" 4.txt ") System.out.println ("time-consuming to copy files through character buffer streams: + (System.currentTimeMillis ()-time4)); long time5 = System.currentTimeMillis (); copyFileByChannel (source, target +" 5.txt "); System.out.println (" time-consuming to copy files through JAVA NIO channels (indirect buffer): + (System.currentTimeMillis ()-time5)); long time6 = System.currentTimeMillis () CopyFileByChannelBufferd (source, target + "6.txt"); System.out.println ("time-consuming to copy files through JAVA NIO channel (direct buffer): + (System.currentTimeMillis ()-time6)); long time7 = System.currentTimeMillis (); copyFileByChannelTransfer (source, target +" 7.txt "); System.out.println (" time-consuming to copy files via JAVA NIO channel: "+ (System.currentTimeMillis ()-time7) Long time8 = System.currentTimeMillis (); copyFileByChannelTransfer (source, target + "8.txt"); System.out.println ("it takes time to copy files through JAVA NIO channel transfer 2: + (System.currentTimeMillis ()-time8)); Thank you for reading! This is the end of this article on "how to transfer a copy file through JAVA NIO channel". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report