This repository was archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileUtils.scala
73 lines (59 loc) · 2.17 KB
/
FileUtils.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* java-tron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* java-tron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.utils
import java.io.{File, IOException}
import java.nio.file._
import java.nio.file.attribute.BasicFileAttributes
import java.util
object FileUtils {
def getRelativeDirectory(): File = {
new File(".").getCanonicalFile
}
def recursiveList(file: File): util.List[String] = {
recursiveList(file.getAbsolutePath)
}
def recursiveList(path: String): util.List[String] = {
val file = new File(path)
require(file.exists(), "path must exist")
val files = new util.ArrayList[String]
Files.walkFileTree(file.toPath, new FileVisitor[Path]() {
def preVisitDirectory(dir: Path, attrs: BasicFileAttributes) = FileVisitResult.CONTINUE
def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = {
files.add(file.toString)
FileVisitResult.CONTINUE
}
def visitFileFailed(file: Path, exc: IOException) = FileVisitResult.CONTINUE
def postVisitDirectory(dir: Path, exc: IOException) = FileVisitResult.CONTINUE
})
files
}
def recursiveDelete(file: File): Boolean = {
recursiveDelete(file.getAbsolutePath)
}
def recursiveDelete(path: String): Boolean = {
val file = new File(path)
if (file.exists) { // check if the file is a directory
if (file.isDirectory) if (file.list.length > 0) {
for (s <- file.list) { // call deletion of file individually
recursiveDelete(s"$path${System.getProperty("file.separator")}$s")
}
}
file.setWritable(true)
file.delete()
} else {
false
}
}
}