{"id":936,"date":"2018-09-17T18:37:49","date_gmt":"2018-09-17T09:37:49","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=936"},"modified":"2019-10-04T02:02:07","modified_gmt":"2019-10-03T17:02:07","slug":"file-copy","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/file-copy","title":{"rendered":"Java file copy (change buffer size)"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Java file copy (change buffer size)<\/a><\/h2>\n<p>\n\u3000In Java <strong>InputStream<\/strong>, ,<strong>OutputStream<\/strong> at<br \/>\nIntroducing a sample program that performs copy processing using input\/output streams.<br \/>\nIn the copy process using streams, even large files can be copied relatively quickly by increasing the read buffer size.<br \/>In the sample program, the buffer size for reading data can be specified as an argument.\n<\/p>\n<p>\n\u3000Since the specified read buffer area is allocated in the JavaVM heap, it is inefficient to make it too large. Therefore, we also check for an efficient data read buffer size when copying files.\n<\/p>\n<h2 class=\"common_title\"><a name=\"1\">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\u30b3\u30d4\u30fc\u5148\u306e\u30d1\u30b9[destPath]\u3078\u30d5\u30a1\u30a4\u30eb\u306e\u30b3\u30d4\u30fc<br \/>\n * \u3092\u884c\u3044\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 * @param bufferSize    \u30c7\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f\u30d0\u30c3\u30d5\u30a1\u30b5\u30a4\u30ba(KB)\u3067\u3059\u3002<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 copyStream(String srcPath, String destPath,<br \/>\n    int bufferSize) throws IOException {<br \/>\n    InputStream in = new FileInputStream(srcPath);<br \/>\n    OutputStream os = new FileOutputStream(destPath);<br \/>\n    copyStream(in, os, bufferSize);<br \/>\n}<\/p>\n<p>\/**<br \/>\n * Writes data from the input stream to the output stream.<br \/>\n *The input\/output stream will be closed after the copy process is completed.<br \/>\n * @param in input stream<br \/>\n * @param os output stream<br \/>\n * @param bufferSize Data read buffer size (KB).<br \/>\n * @throws IOException If some input\/output processing exception occurs<br \/>\n *\/<br \/>\npublic static void copyStream(InputStream in, OutputStream os,<br \/>\n    int bufferSize) throws IOException {<br \/>\n    int len = -1;<br \/>\n    byte[] b = new byte[bufferSize * 1024];<br \/>\n    try {<br \/>\n        while ((len = in.read(b, 0, b.length)) != -1) {<br \/>\n            os.write(b, 0, len);<br \/>\n        }<br \/>\n        os.flush();<br \/>\n    } finally {<br \/>\n        if (in != null) {<br \/>\n            try {<br \/>\n                in.close();<br \/>\n            } catch (IOException e) {<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n        }<br \/>\n        if (os != null) {<br \/>\n            try {<br \/>\n                os.close();<br \/>\n            } catch (IOException e) {<br \/>\n                e.printStackTrace();<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n<\/textarea><\/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=\"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. In the sample program, <i>C:\\100M.txt<\/i> and read data in units of 1000KB buffer size <i>C:\\a.txt<\/i>  Copy to<\/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        copyStream(\"C:\\\\100M.txt\", \"C:\\\\a.txt\",1000);\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<h2 class=\"common_title\"><a name=\"4\">Change buffer size<\/a><\/h2>\n<p>\nTo check the efficient data read buffer size when copying files, use the third argument of copyStream as<br \/>\nThe results of similar copy processing in 1KB, 100KB, 1000KB, and 10000KB units are summarized below.<br \/>\n<img src=\"https:\/\/chat-messenger.com\/images\/file_copy_stream_reslt.jpg\" alt=\"\" border=\"0\"><br clear=\"all\"><br \/>\n<br \/>\nAlthough it cannot be concluded from the above results alone, if you specify the data read buffer size in units of 1000KB,<br \/>\nThe results showed that it was efficient in terms of heap usage and processing time during copy processing.<br \/>\nSimilar results were obtained when the file size was increased (approximately 1GB).<\/p>\n<p>On the other hand, if you are copying a small file (1MB or less), it is better to specify a buffer size of 100KB or so, since a large buffer size will be useless.\n<\/p>\n\n<h2 class=\"common_title\"><a name=\"5\">Use FileChannel for more speed.<\/a><\/h2>\n<p><a name=\"5\"><\/a><\/p>\n<p><a name=\"5\"><br \/>\n\u3000<strong>InputStream<\/strong>, ,<strong>OutputStream<\/strong><br \/>\nCopy processing is important in understanding input\/output streams, but<br \/>\nNew I\/O introduced in J2SE1.4 <strong>FileChannel#transferTo<\/strong><br \/>\nmethod can be used to copy files even more efficiently.<br \/>\nNext, we will show you how to use FileChannel for easy and fast file copying.<br \/>\n\u21d2 <\/a><a href=\"https:\/\/chat-messenger.com\/en\/blog\/java\/filechannel-transferto\/\"><strong>Java File Copy (easy and fast)<\/strong><\/a><br \/>\n<\/p>","protected":false},"excerpt":{"rendered":"<p>Java file copy (change buffer size) java.io.InputStream in Java, ja [\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\/936"}],"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=936"}],"version-history":[{"count":6,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/936\/revisions"}],"predecessor-version":[{"id":4541,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/936\/revisions\/4541"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}