File input/output: Difference between revisions

Line 910:
 
The files will automatically be closed on exit of their ''with:'' blocks. (Thus even if an I/O error occurred while reading the middle of the input file we are assured that the ''.close()'' method will have been called on each of the two files.
 
=={{header|R}}==
If files are textual we can use <tt>readLines</tt> ("-1" means "read until the end")
 
<lang R>src <- file("input.txt", "r")
dest <- file("output.txt", "w")
 
fc <- readLines(src, -1)
writeLines(fc, dest)
close(src); close(dest)</lang>
 
If the files are not textual but "generic":
 
<lang R>src <- file("input.txt", "rb")
dest <- file("output.txt", "wb")
 
while( length(v <- readBin(src, "raw")) > 0 ) {
writeBin(v, dest)
}
close(src); close(dest)</lang>
 
Another simpler way is to use <tt>file.copy</tt>
 
<lang R>file.copy("input.txt", "output.txt", overwrite = FALSE)</lang>
 
 
=={{header|RapidQ}}==