This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.foo.policy;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.util.ArrayList;import java.util.ConcurrentModificationException;import java.util.Iterator;import java.util.List;import org.osgi.framework.BundleActivator;import org.osgi.framework.BundleContext;import org.osgi.framework.BundleException;import org.osgi.framework.ServiceReference;import org.osgi.service.condpermadmin.ConditionalPermissionAdmin;import org.osgi.service.condpermadmin.ConditionalPermissionInfo;import org.osgi.service.condpermadmin.ConditionalPermissionUpdate;public class Activator implements BundleActivator{ public void start(BundleContext context) throws Exception { File policyFile = getPolicyFile(context); List<String> encodedInfos = readPolicyFile(policyFile); encodedInfos.add(0, "ALLOW {" + "[org.osgi.service.condpermadmin.BundleLocationCondition \"" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def shouldThrow[E<:Throwable](f: =>Unit) { try { f fail("should have thrown an Exception") } catch { case e:E => () case e => throw e } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibFrom(a: Int, b: Int): Stream[Int] = a #:: fibFrom(b, a + b) | |
def fibonacciStream(n: Int) = fibFrom(1, 1).take(n).toList.last | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.scalacheck.Gen | |
import org.scalatest.FunSuite | |
import org.scalacheck.Prop.forAll | |
import org.scalatest.prop.Checkers | |
class FizzBuzzTest extends FunSuite with Checkers { | |
test("fizzbuzz") { | |
val fb = fizzbuzz(100) | |
val range = Gen.choose[Int](1, 100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(s: Int): List[String] = { | |
// helper method inspired by haskell, cycle a list infinitely, | |
def cycle(xs: List[String]): Stream[String] = Stream.continually(xs).flatten | |
val numbers = Stream.from(1) | |
// a infinite cycle of "", "", "Fizz" | |
val fizzes = cycle(List("", "", "Fizz")) | |
// a infinite cycle of "", "", "", "", "Buzz" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// helper method inspired by haskell, cycle a list infinitely, | |
def cycle(xs: List[String]): Stream[String] = Stream.continually(xs).flatten | |
val numbers = Stream.from(1) | |
// a infinite cycle of "", "", "Fizz" | |
val fizzes = cycle(List("", "", "Fizz")) | |
// a infinite cycle of "", "", "", "", "Buzz" | |
val buzzes = cycle(List("", "", "", "", "Buzz")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sumLength(xs: List[Int]) = xs.foldLeft(None:Option[(Int,Int)]){ | |
case (None, x) => Some(x, 1) | |
case (Some((s:Int, l:Int)), x) => Some((s + x, l + 1 )) | |
} | |
def avg(xs: List[Int]): Option[Int] = sumLength(xs).map{case (sum, length) => sum / length} | |
val avg1 = avg(List(1, 2, 3, 4, 6, 7, 8, 9)) | |
val avg2 = avg(List(1, 2, 3, 4, 5, 6, 7, 8, 9)) | |
val avg3 = avg(List.empty[Int]) |
OlderNewer