summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-08-16 23:58:21 +0000
committerPaul Phillips <paulp@improving.org>2009-08-16 23:58:21 +0000
commit309e7e0b58df706ef5764f8e16e0c4880eea7232 (patch)
tree92d75f3fa5f626482102ac75316d867bf572e5cf /src
parent9cea5f6198e89f886768a45f304dfaa8f769784f (diff)
downloadscala-309e7e0b58df706ef5764f8e16e0c4880eea7232.tar.gz
scala-309e7e0b58df706ef5764f8e16e0c4880eea7232.tar.bz2
scala-309e7e0b58df706ef5764f8e16e0c4880eea7232.zip
Adding more and fleshing out convenience functi...
Adding more and fleshing out convenience functions in io.File.
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/io/File.scala103
1 files changed, 59 insertions, 44 deletions
diff --git a/src/library/scala/io/File.scala b/src/library/scala/io/File.scala
index e6d3118286..b2ba62dc36 100644
--- a/src/library/scala/io/File.scala
+++ b/src/library/scala/io/File.scala
@@ -10,7 +10,7 @@
package scala.io
-import java.io.{ FileInputStream, FileOutputStream, BufferedWriter, OutputStreamWriter, File => JFile }
+import java.io.{ FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter, File => JFile }
import collection.Traversable
object File
@@ -40,61 +40,76 @@ class File(val file: JFile) extends collection.Iterable[File]
/** Convenience function for iterating over the lines in the file.
*/
- def lines: Iterator[String] = toSource.getLines()
-
- /** Deletes the file or directory recursively. Returns false if it failed.
- * Use with caution!
- */
- def deleteRecursively(): Boolean = deleteRecursively(file)
- private def deleteRecursively(f: JFile): Boolean = {
- if (f.isDirectory) file.listFiles match {
- case null =>
- case xs => xs foreach deleteRecursively
+ def lines(implicit codec: Codec = Codec.default): Iterator[String] = toSource(codec).getLines()
+
+ /** Convenience function to import entire file into a String.
+ */
+ def slurp(implicit codec: Codec = Codec.default) = toSource(codec).mkString
+
+ /** Deletes the file or directory recursively. Returns false if it failed.
+ * Use with caution!
+ */
+ def deleteRecursively(): Boolean = deleteRecursively(file)
+ private def deleteRecursively(f: JFile): Boolean = {
+ if (f.isDirectory) file.listFiles match {
+ case null =>
+ case xs => xs foreach deleteRecursively
}
f.delete()
}
- /** Obtains an InputStream. */
- def inputStream = new FileInputStream(file)
-
- /** Obtains a OutputStream. */
- def outputStream(append: Boolean = false) = new FileOutputStream(file, append)
-
- /** Obtains an OutputStreamWriter wrapped around a FileOutputStream.
- * This should behave like a less broken version of java.io.FileWriter,
- * in that unlike the java version you can specify the encoding.
- */
- def writer(append: Boolean = false)(implicit codec: Codec = Codec.default) =
- new OutputStreamWriter(outputStream(append), codec.charSet)
-
- /** Wraps a BufferedWriter around the result of writer().
- */
- def bufferedWriter(append: Boolean = false)(implicit codec: Codec = Codec.default) =
- new BufferedWriter(writer(append)(codec))
-
- /** Writes all the Strings in the given iterator to the file. */
- def writeAll(xs: Traversable[String], append: Boolean = false)(implicit codec: Codec = Codec.default): Unit = {
- val out = bufferedWriter(append)(codec)
- xs foreach (out write _)
- out close
+ /** Obtains an InputStream. */
+ def inputStream() = new FileInputStream(file)
+
+ /** Obtains a OutputStream. */
+ def outputStream(append: Boolean = false) = new FileOutputStream(file, append)
+
+ /** Obtains an InputStreamReader wrapped around a FileInputStream.
+ */
+ def reader(implicit codec: Codec = Codec.default) =
+ new InputStreamReader(inputStream, codec.charSet)
+
+ /** Obtains an OutputStreamWriter wrapped around a FileOutputStream.
+ * This should behave like a less broken version of java.io.FileWriter,
+ * in that unlike the java version you can specify the encoding.
+ */
+ def writer(append: Boolean = false)(implicit codec: Codec = Codec.default) =
+ new OutputStreamWriter(outputStream(append), codec.charSet)
+
+ /** Wraps a BufferedReader around the result of reader().
+ */
+ def bufferedReader(implicit codec: Codec = Codec.default) =
+ new BufferedReader(reader(codec))
+
+ /** Wraps a BufferedWriter around the result of writer().
+ */
+ def bufferedWriter(append: Boolean = false)(implicit codec: Codec = Codec.default) =
+ new BufferedWriter(writer(append)(codec))
+
+ /** Writes all the Strings in the given iterator to the file. */
+ def writeAll(xs: Traversable[String], append: Boolean = false)(implicit codec: Codec = Codec.default): Unit = {
+ val out = bufferedWriter(append)(codec)
+ xs foreach (out write _)
+ out close
}
- /** Attempts to return the file extension. */
- def extension = file.getName match {
- case extensionRegex(x) => Some(x)
- case _ => None
+ /** Attempts to return the file extension. */
+ def extension = file.getName match {
+ case extensionRegex(x) => Some(x)
+ case _ => None
}
/** Creates a Source from this file. */
- def toSource = Source.fromFile(file)()
+ def toSource(implicit codec: Codec = Codec.default): Source =
+ (Source fromFile file)(codec)
/** Creates a new File with the specified path appended. */
- def /(child: String) = new File(new JFile(file, child))
+ def /(child: String) = new File(new JFile(file, child))
- override def toString() = file.toString
- override def equals(other: Any) = other match {
- case x: File => this.file == x.file
- case _ => false
+ override def toString() = file.toString
+ override def equals(other: Any) = other match {
+ case x: File => this.file == x.file
+ case _ => false
}
override def hashCode = file.hashCode
}