@@ -20,6 +20,9 @@ package object files {
20
20
* Scala wrapper for java.io.File
21
21
*/
22
22
case class File (file : JFile ) {
23
+
24
+ def toJava : JFile = file
25
+
23
26
def path : Path = file.toPath
24
27
25
28
def fullPath : String = path.toString
@@ -43,14 +46,16 @@ package object files {
43
46
44
47
def contentType : Option [String ] = Option (Files .probeContentType(path))
45
48
46
- def parent : File = file.getParentFile
49
+ def parent : File = file.getParentFile.toScala
47
50
48
- def / (child : String ): File = new JFile (file, child)
51
+ def / (child : String ): File = new JFile (file, child).toScala
49
52
50
53
def / (f : File => File ): File = f(this )
51
54
52
55
def createIfNotExists (): File = if (file.exists()) this else {parent.mkdirs(); Files .createFile(path)}
53
56
57
+ def exists : Boolean = file.exists()
58
+
54
59
def content (implicit codec : Codec ): BufferedSource = Source .fromFile(file)(codec)
55
60
def source (implicit codec : Codec ): BufferedSource = content(codec)
56
61
@@ -59,7 +64,7 @@ package object files {
59
64
Iterator .continually(stream.read()).takeWhile(- 1 != ).map(_.toByte) // TODO: close the stream
60
65
}
61
66
62
- def mkdir (): File = {file.mkdirs(); this }
67
+ def mkdirs (): File = {file.mkdirs(); this }
63
68
64
69
def chars (implicit codec : Codec ): Iterator [Char ] = content(codec)
65
70
@@ -117,7 +122,7 @@ package object files {
117
122
// def hide(): Boolean = ???
118
123
// def unhide(): Boolean = ???
119
124
120
- def list : Files = file.listFiles().toIterator map javaToFile
125
+ def list : Files = file.listFiles().toIterator. map(_.toScala)
121
126
def children : Files = list
122
127
123
128
def listRecursively (maxDepth : Int = Int .MaxValue ): Files = Files .walk(path, maxDepth)
@@ -223,7 +228,7 @@ package object files {
223
228
def sameFileAs (that : File ): Boolean = Files .isSameFile(this .path, that.path)
224
229
225
230
/**
226
- * @return true if this file is exactly same as that file TODO: recursively for directories
231
+ * @return true if this file is exactly same as that file TODO: recursively for directories (or empty files?)
227
232
*/
228
233
def contentSameAs (that : File ): Boolean = this .bytes sameElements that.bytes
229
234
val `===` = contentSameAs _
@@ -296,13 +301,17 @@ package object files {
296
301
def ls (file : File ): Files = file.list
297
302
def ls_r (file : File ): Files = file.listRecursively()
298
303
def touch (file : File ): File = file.touch()
299
- def mkdir (file : File ): File = file.mkdir ()
304
+ def mkdir (file : File ): File = file.mkdirs ()
300
305
def chown (owner : String , file : File ): File = file.setOwner(owner)
301
306
def chgrp (group : String , file : File ): File = file.setGroup(group)
302
307
def chmod_+ (permission : PosixFilePermission , file : File ): File = file.addPermissions(permission)
303
308
def chmod_- (permission : PosixFilePermission , file : File ): File = file.removePermissions(permission)
304
309
}
305
310
311
+ implicit class FileOps (file : JFile ) {
312
+ def toScala : File = File (file)
313
+ }
314
+
306
315
implicit class InputStreamOps (in : InputStream ) {
307
316
def > (out : OutputStream ): Unit = pipeTo(out)
308
317
@@ -344,9 +353,8 @@ package object files {
344
353
}
345
354
346
355
implicit def codecToCharSet (codec : Codec ): Charset = codec.charSet
347
- implicit def pathToFile (path : Path ): File = path.toFile
348
- implicit def javaToFile (file : JFile ): File = File (file) // TODO: ISO micro-macros
349
- implicit def toJavaFile (file : File ): JFile = file.file // TODO: Move implicit converters to a special import?
356
+
357
+ implicit def pathToFile (path : Path ): File = path.toFile.toScala
350
358
351
359
private [files] implicit def pathStreamToFiles (files : JStream [Path ]): Files = files.iterator().map(pathToFile)
352
360
private [files] def when [A ](condition : Boolean )(f : => A ): Option [A ] = if (condition) Some (f) else None
0 commit comments