Abduqodiri Qurbonzoda [JB]
09/19/2024, 10:32 AMEugen Martynov
09/19/2024, 2:20 PMPath.walk
default is depth first? I would probably highlight it in documentation. "Include directories" means that it is not going to walk subfolders by default, correct?Eugen Martynov
09/19/2024, 2:22 PMvisitFileTree()
doesn't have breadth first option analogueKlitos Kyriacou
09/19/2024, 2:26 PM"Include directories" means that it is not going to walk subfolders by default, correct?The way I've understood the documentation at first glance was that it would walk subfolders (and always would, no matter what options you gave) and that the "include directories" option would determine whether or not directory names would be returned in the results.
Klitos Kyriacou
09/19/2024, 2:26 PMKlitos Kyriacou
09/19/2024, 2:32 PM->
operator is split in two lines.Klitos Kyriacou
09/19/2024, 2:41 PMdeleteRecursively
doesn't have an onError function like copyToRecursively
. The OnErrorResult
class seems well suited for that too.CLOVIS
09/19/2024, 2:50 PMPaulo Cereda
09/19/2024, 3:26 PMEugen Martynov
09/19/2024, 5:12 PMAdam S
09/19/2024, 8:54 PMPath.walk()
is significantly less useful than File.walk()
• It's missing onEnter {}
, so I can't dynamically prevent entering specific directories.
• Can't do maxDepth(5)
to limit the depth.Adam S
09/19/2024, 8:58 PMPath.visitFileTree()
build stuff is not very helpful.
E.g. onPreVisitDirectory. I don't think this doc practically explains the purpose and intended usage of the function:
Overrides the corresponding function of the built file visitor with the provided function.
Adam S
09/19/2024, 9:10 PMfileVisitor()
kdoc. It has
> ``` kotlin
but it should be
> ```kotlin
https://github.com/JetBrains/kotlin/blob/99b03cef10a0ebb63f55bf6a16390c28c1e96bbe/libraries/stdlib/jdk7/src/kotlin/io/path/PathUtils.kt#L1155C2-L1155C14
Because of the extra space the code block isn't syntax highlighted https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.io.path/file-visitor.htmlAdam S
09/19/2024, 9:17 PMPath.visitFileTree()
can't be converted to a sequence :(
sequence {
myLocalPath.visitFileTree(maxDepth = 5) {
onPreVisitDirectory { dir, _ ->
if (dir.name != "foo") {
CONTINUE
} else {
SKIP_SUBTREE
}
}
onVisitFile { file, _ ->
yield(file) // ERROR [NON_LOCAL_SUSPENSION_POINT] Suspension functions can be called only within coroutine body
CONTINUE
}
}
}
Chris Lee
09/20/2024, 12:12 AMlist
, the others visitors are variations on that.Klitos Kyriacou
10/11/2024, 1:42 PMFiles.walk
should be done in a try-with-resources block because the returned Stream<Path>
needs to be closed. The new Kotlin Path.walk
function returns a Sequence<Path>
which is not AutoCloseable
. How do we release the resources from this sequence?Abduqodiri Qurbonzoda [JB]
10/11/2024, 1:50 PMPath.walk
does not keep directories “open”, meaning it does not maintain `DirectoryStream`s. Therefore, there is no need to close them. See the implementation for details: https://github.com/JetBrains/kotlin/blob/256710895084b3a5d3ed97b16d208e1e6c1fc276/libraries/stdlib/jdk7/src/kotlin/io/path/PathTreeWalk.ktKlitos Kyriacou
10/11/2024, 2:05 PMPath.walk
on a small device with very limited memory with a TCP connection to a server and reading a directory that contains a million files. It will have to read all of them in one go into a List
.Abduqodiri Qurbonzoda [JB]
10/14/2024, 2:06 PMFilipp Zhinkin
03/07/2025, 5:46 PMI haven't used these functions, but I'm wondering why@Klitos Kyriacou do you see any practical strategies to deal with errors occurred during removal (or, maybe, you have a scenario where it will be useful)? Or it's mostly about the coherence of these two functions?doesn't have an onError function likedeleteRecursively
. ThecopyToRecursively
class seems well suited for that too.OnErrorResult
Klitos Kyriacou
03/10/2025, 10:55 AMcopyToRecursively
and for better usability. On the latter point, I've seen many cases in Java where a less experienced developer used File.delete instead of Files.delete and had problems due to failing to detect that File.delete was unable to delete the file (or they knew that the file could not be deleted but didn't know why). In the case of deleteRecursively
what should be done if a file in the middle of a list could not be deleted? Should it terminate immediately with an exception? Should it continue deleting the remaining other files and then throw the exception? Should it continue deleting the remaining files and swallow the exception? An onError
function (with a sensible default such as `copyToRecursively`'s default) would give the developer a choice, and would also allow the developer to log a list of files that failed to be deleted, together with the reason for failure for each file.
By the way, I've just noticed that the copyToRecursively
return value is not documented.Filipp Zhinkin
03/10/2025, 3:38 PM