How it works...

You can run the sample code to verify that, after invoking the copyTo() extension function, both files contain the same text content. In our case, we get the following output:

"Testing can show the presence of errors, but not their absence." - E. W. Dijkstra
"Testing can show the presence of errors, but not their absence." - E. W. Dijkstra

Under the hood, the copyTo() function reads InputStream in the source file to the buffer and writes it to the OutputStream target file. Internally, streams are being accessed inside the use() function block, which closes them automatically after the operation finishes.

Apart from the target File instance, the copyTo() function takes two optional parameters—overwrite: Boolean, which is set to false by default, and bufferSize: Int, which is assigned to the default value. Keep in mind that, whenever some directories on a way to the target file are missing, they will be created. Also, if the target file already exists, the copyTo() function will fail, unless the override argument is set to true

  • When the overwrite parameter is set to true and target points to a directory, it will be replaced only if it is empty. 
  • If you invoke copyTo() on a File instance that points to a directory, it will be copied without its content. Only an empty directory will be created under the target path. 
  • The copyTo() function doesn't preserve copied file attributes, that is, the creation/modification date and permissions.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.203.86