{"id":253,"date":"2015-07-10T21:41:46","date_gmt":"2015-07-10T11:41:46","guid":{"rendered":"https:\/\/learn-java-by-example.com\/?p=253"},"modified":"2015-11-20T17:28:24","modified_gmt":"2015-11-20T06:28:24","slug":"copy-file-java-6","status":"publish","type":"post","link":"https:\/\/learn-java-by-example.com\/java\/copy-file-java-6\/","title":{"rendered":"How to copy a file using Java 6"},"content":{"rendered":"

Prior to Java 7, Java did not provide a standard method to copy a file. To implement copying you needed to read all bytes from source file and write to destination. The read() method will return -1 when eof is reached, otherwise it returns the number of bytes read.<\/p>\n

\r\npublic static void copy(File source, File destination)\r\n   throws IOException\r\n{\r\n   \/\/ Open file to be copied\r\n   InputStream in = new FileInputStream(source);\r\n\r\n   \/\/ And where to copy it to\r\n   OutputStream out = new FileOutputStream(destination);\r\n\r\n   \/\/ Read bytes and write to destination until eof\r\n\r\n   byte[] buf = new byte[1024];\r\n   int len = 0;\r\n   while ((len = in.read(buf)) >= 0)\r\n   {\r\n      out.write(buf, 0, len);\r\n   }\r\n\r\n   \/\/ close both streams\r\n\r\n   in.close();\r\n   out.close();\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

Prior to Java 7, Java did not provide a standard method to copy a file. To implement copying you needed to read all bytes from source file and write to destination. The read() method will return -1 when eof is reached, otherwise it returns the number of bytes read.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_options":[]},"categories":[4,89],"tags":[79,77,78,76],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6Yyl2-45","jetpack-related-posts":[{"id":335,"url":"https:\/\/learn-java-by-example.com\/java\/read-text-file-line-line\/","url_meta":{"origin":253,"position":0},"title":"How do I read a text file line by line?","date":"November 20, 2013","format":false,"excerpt":"The readLine() method of java.io.BufferedReader class reads the next line from a text file. When it reaches the end of the file it will return null.","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":339,"url":"https:\/\/learn-java-by-example.com\/java\/scanner-read-words-text-file\/","url_meta":{"origin":253,"position":1},"title":"Using Scanner to read words from text file","date":"November 20, 2014","format":false,"excerpt":"Often you need to read a file line by line. Alternatively sometimes you want to read text word by word (for example to count the occurrence of different words). The Scanner classes next() method can be used for this as shown in the following example. You can find an example\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":96,"url":"https:\/\/learn-java-by-example.com\/java\/simple-mortgage-calculator\/","url_meta":{"origin":253,"position":2},"title":"Simple Mortgage Calculator","date":"June 5, 2010","format":false,"excerpt":"This example expands on the Monthly Payment Calculator example we posted earlier. As well as calculating the monthly payment for a loan, it then goes on to use that to calculate the balance of the loan after each payment. This is often referred to as an amortisation schedule. The displayMonthlyBalance()\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":205,"url":"https:\/\/learn-java-by-example.com\/java\/counting-numbers\/","url_meta":{"origin":253,"position":3},"title":"Counting numbers","date":"September 12, 2011","format":false,"excerpt":"The assignment here is to read a text file containing a list of numbers. We need to report how many numbers were in the file, the sum of all the numbers, the average of all numbers, and the minimum and maximum number. First step is to read the file and\u2026","rel":"","context":"In "Featured"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":133,"url":"https:\/\/learn-java-by-example.com\/java\/fibonnaci-number\/","url_meta":{"origin":253,"position":4},"title":"Fibonnaci Number","date":"December 3, 2010","format":false,"excerpt":"Generating Fibonacci numbers is an assignment often given to Java students when they are being introduced to recursion. Recursion is programming technique that involves a method calling itself to solve a problem. When implementing a recursive solution we look for two things: A base case that returns a value without\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":160,"url":"https:\/\/learn-java-by-example.com\/java\/pascals-triangle\/","url_meta":{"origin":253,"position":5},"title":"Pascals Triangle","date":"February 13, 2011","format":false,"excerpt":"Write a Java application that prints the first 10 lines of Pascals Triangle. Each row of a Pascals Triangle can be calculated from the previous row so the core of the solution is a method that calculates a row based on the previous row which is passed as input. Once\u2026","rel":"","context":"In "Java"","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/253"}],"collection":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/comments?post=253"}],"version-history":[{"count":2,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":336,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/posts\/253\/revisions\/336"}],"wp:attachment":[{"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/media?parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/categories?post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-java-by-example.com\/wp-json\/wp\/v2\/tags?post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}