{"id":939,"date":"2018-09-17T18:39:34","date_gmt":"2018-09-17T09:39:34","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=939"},"modified":"2018-12-23T01:53:23","modified_gmt":"2018-12-22T16:53:23","slug":"filechannel-transferto","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/filechannel-transferto","title":{"rendered":"Java File Copy (easy and fast)"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Java File Copy (easy and fast)<\/a>FileChannel#transferTo<\/h2>\n<p>\n\u3000In Java<strong>Copy Files<\/strong>The following is a sample program that does the following<br \/>\nThe sample programs are based on the New I\/O <strong>FileChannel#transferTo<\/strong> method.<br \/>\nThis is the simplest coding method, as it allows you to write a program for copy processing without having to be aware of the buffer required for reading data.\n<\/p>\n<h2 class=\"common_title\"><a name=\"1\">What is a channel?<\/a><\/h2>\n<p>\nThe word channel is used in many different ways.<br \/>\nBasically, it seems to indicate a transmission path for inputting and outputting data between other devices.<strong>FileChannel<\/strong> will be a class that represents a connection for reading and writing to a file.<br \/>\n<br \/><strong>transferTo<\/strong> The method can transfer byte data from the source file to the destination channel.<\/p>\n<p>The following is taken from the JavaAPI documentation.\n<\/p>\n<blockquote><p>\n<strong>transferTo(long position, long count, WritableByteChannel target)<\/strong><br \/>\nTransfers bytes from a file on this channel to the specified writable byte channel.<br \/>\nThis method may be much more efficient than a simple loop of reading data from this channel and writing it to the target channel. Many operating systems can transfer bytes directly from the file system cache to the target channel. No bytes are copied at this time.\n<\/p><\/blockquote>\n<p>\nThis verification result shows how to use a normal stream,<br \/>\nThe copy process was faster than other FileChannel methods (such as using the ByteBuffer#allocateDirect method).<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<h2 class=\"common_title\"><a name=\"2\">sample program<\/a><\/h2>\n<p><textarea readonly=\"readonly\" style=\"font-size: 13px;height: 400px;\" class=\"src\" onclick=\"this.focus();this.select()\">\/**<br \/>\n * \u30b3\u30d4\u30fc\u5143\u306e\u30d1\u30b9[srcPath]\u304b\u3089\u3001\u30b3\u30d4\u30fc\u5148\u306e\u30d1\u30b9[destPath]\u3078<br \/>\n * \u30d5\u30a1\u30a4\u30eb\u306e\u30b3\u30d4\u30fc\u3092\u884c\u3044\u307e\u3059\u3002<br \/>\n * \u30b3\u30d4\u30fc\u51e6\u7406\u306b\u306fFileChannel#transferTo\u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3057\u307e\u3059\u3002<br \/>\n * \u5c1a\u3001\u30b3\u30d4\u30fc\u51e6\u7406\u7d42\u4e86\u5f8c\u3001\u5165\u529b\u30fb\u51fa\u529b\u306e\u30c1\u30e3\u30cd\u30eb\u3092\u30af\u30ed\u30fc\u30ba\u3057\u307e\u3059\u3002<br \/>\n * @param srcPath    \u30b3\u30d4\u30fc\u5143\u306e\u30d1\u30b9<br \/>\n * @param destPath    \u30b3\u30d4\u30fc\u5148\u306e\u30d1\u30b9<br \/>\n * @throws IOException    \u4f55\u3089\u304b\u306e\u5165\u51fa\u529b\u51e6\u7406\u4f8b\u5916\u304c\u767a\u751f\u3057\u305f\u5834\u5408<br \/>\n *\/<br \/>\npublic static void copyTransfer(String srcPath, String destPath)<br \/>\n    throws IOException {<\/p>\n<p>    FileChannel srcChannel = new<br \/>\n        FileInputStream(srcPath).getChannel();<br \/>\n    FileChannel destChannel = new<br \/>\n        FileOutputStream(destPath).getChannel();<br \/>\n    try {<br \/>\n        srcChannel.transferTo(0, srcChannel.size(), destChannel);<br \/>\n    } finally {<br \/>\n        srcChannel.close();<br \/>\n        destChannel.close();<br \/>\n    }<\/p>\n<p>}<br \/>\n<\/textarea><\/p>\n<h2 class=\"common_title\"><a name=\"3\">execution (e.g. program)<\/a><\/h2>\n<p>\n\u3000A 100MB file \"100M.txt\" is prepared directly under the C drive for file copying of the sample program.<br \/>\nIn the sample program <i>C:\\100M.txt<\/i> and copy the <i>C:\\a.txt<\/i> file.<\/p>\n<p>The sample was run in the following environment.<br \/>\nOS : WindowsXP<br \/>\nCPU : Athlon 1.46GHz<br \/>\nMemory : 1GB<br \/>\nJRE : 1.6.4\n<\/p>\n<p>\u25c6Example of Execution<\/p>\n<pre class=\"sample_src\">\/**\r\n * Execution example\r\n * @param args\r\n *\/\r\npublic static void main(String[] args) {\r\n    try {\r\n        copyTransfer(\"C:\\\\100M.txt\", \"C:\\\\a.txt\"); }\r\n    } catch (IOException e) {\r\n        e.printStackTrace(); }\r\n    }\r\n}\r\n<\/pre>\n<p>\u25c6Execution Result<br \/>\n<a href=\"https:\/\/chat-messenger.com\/images\/file_copy.jpg\" target=\"_blank\"><br \/>\n<img src=\"https:\/\/chat-messenger.com\/images\/file_copy.jpg\" alt=\"\" border=\"0\"><\/a><br clear=\"all\"><\/p>\n<p>\n\u3000<strong>FileChannel#transferTo<\/strong> In copy processing using the method<br \/>\nCopying a 100MB file was completed in about 3 seconds. Also, the heap usage during the copy process was about 300 (KB).<\/p>\n<p>By the way,<a href=\"https:\/\/chat-messenger.com\/en\/blog\/java\/file-copy\/\">Java file copy (change buffer size)<\/a>In the method introduced in<br \/>\nWhen we specified the most efficient buffer size of 1000KB, the processing time was about 4 seconds and the heap usage was about 1,300KB.\n<\/p>\n<p>\nEven file copying by stream can be sped up by tuning the buffer size, so<br \/>\nIt can be said that there is not much difference in the process of copying a single file.<br \/>\nHowever, FileChannel can be effective in terms of processing speed and heap usage when large files are copied in succession or when multiple copy operations are performed in parallel.<\/p>","protected":false},"excerpt":{"rendered":"<p>Java file copy (easy\/fast) FileChannel#transferTo Copy files with Java [\u2026]<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/939"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=939"}],"version-history":[{"count":8,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/939\/revisions"}],"predecessor-version":[{"id":1883,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/939\/revisions\/1883"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=939"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=939"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=939"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}