-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/scala/com/github/mrpowers/spark/daria/utils/NioUtils.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.github.mrpowers.spark.daria.utils | ||
|
||
import java.io.IOException | ||
import java.nio.file.{Files, Paths, Path, SimpleFileVisitor, FileVisitResult} | ||
import java.nio.file.attribute.BasicFileAttributes | ||
|
||
// Copied from StackOverflow: https://stackoverflow.com/a/47380288/1125159 | ||
// Per Vladimir Matveev, it's best to use the Java NIO.2 API for modern Java I/O | ||
object NioUtils { | ||
|
||
def remove(root: Path, deleteRoot: Boolean = true): Unit = | ||
Files.walkFileTree( | ||
root, | ||
new SimpleFileVisitor[Path] { | ||
override def visitFile(file: Path, attributes: BasicFileAttributes): FileVisitResult = { | ||
Files.delete(file) | ||
FileVisitResult.CONTINUE | ||
} | ||
|
||
override def postVisitDirectory(dir: Path, exception: IOException): FileVisitResult = { | ||
if (deleteRoot) Files.delete(dir) | ||
FileVisitResult.CONTINUE | ||
} | ||
} | ||
) | ||
|
||
def removeUnder(string: String): Unit = remove(Paths.get(string), deleteRoot = false) | ||
|
||
def removeAll(string: String): Unit = remove(Paths.get(string)) | ||
|
||
def removeUnder(file: java.io.File): Unit = remove(file.toPath, deleteRoot = false) | ||
|
||
def removeAll(file: java.io.File): Unit = remove(file.toPath) | ||
|
||
} |