Scala script to copy files
Solution 1
For performance reasons it is better to use java.nio.Channel to do the copying.
Listing of copy.scala:
import java.io.{File,FileInputStream,FileOutputStream}
val src = new File(args(0))
val dest = new File(args(1))
new FileOutputStream(dest) getChannel() transferFrom(
new FileInputStream(src) getChannel, 0, Long.MaxValue )
To try this out create a file called test.txt with the following content:
Hello World
After creating test.txt, run the following from the command line:
scala copy.scala test.txt test-copy.txt
Verify that test-copy.txt has Hello World
as its content.
Solution 2
Why not use Apache Commons IO and FileUtils.copyFile() in particular ? Note that FileUtils has a large number of methods to copy files/directories etc.
Solution 3
Java 7 is now out and you have another option: java.nio.file.Files.copy
. The probably easiest solution (And with Scalas superior import
even easier). Provided that from
and to
are strings as in your question:
import java.nio.file.StandardCopyOption.REPLACE_EXISTING
import java.nio.file.Files.copy
import java.nio.file.Paths.get
implicit def toPath (filename: String) = get(filename)
copy (from, to, REPLACE_EXISTING)
Of course you should start using java.nio.file.Paths
instead of strings.
Solution 4
If you really want to do it yourself instead of using a library like commons-io, you can do the following in version 2.8. Create a helper method "use". It will give you a form of automatic resource management.
def use[T <: { def close(): Unit }](closable: T)(block: T => Unit) {
try {
block(closable)
}
finally {
closable.close()
}
}
Then you can define a copy method like this:
import java.io._
@throws(classOf[IOException])
def copy(from: String, to: String) {
use(new FileInputStream(from)) { in =>
use(new FileOutputStream(to)) { out =>
val buffer = new Array[Byte](1024)
Iterator.continually(in.read(buffer))
.takeWhile(_ != -1)
.foreach { out.write(buffer, 0 , _) }
}
}
}
Note that the buffer size (here: 1024) might need some tuning.
Solution 5
Hire sbt.IO. It's pure scala, it can copy only changed files, has usefull routines like copyDirectory
, delete
, listFiles
etc . You can use it as follow:
import sbt._
IO.copyFile(file1, file2)
Note you should add proper dependency:
libraryDependencies += "org.scala-sbt" % "io" % "0.13.0"
EDIT:
Actually this is not a good approach since dependency "org.scala-sbt" % "io" % "version"
was compiled using particular scala version and for now you cannot use it with 2.10.X scala version. But maybe in future you will can add double %% in you dependency like "org.scala-sbt" %% "io" % "version"
and it will work...
kulkarni
Updated on December 14, 2020Comments
-
kulkarni over 3 years
I want to copy file a.txt to newDir/ from within a scala script. In java this would be done by creating 2 file streams for the 2 files, reading into buffer from a.txt and writing it to the FileOutputStream of the new file. Is there a better way to achieve this in scala? May be something in scala.tools.nsc.io._. I searched around but could not find much.