Kotlin: get list of all files in resource folder - kotlin

Is there a way to get list of all files in "resources" folder in Kotlin?
I can read specific file as
Application::class.java.getResourceAsStream("/folder/filename.ext")
But sometimes I just want to extract everything from folder "folder" to an external directory.

As I struggled with the same issue and couldn't find a concrete answer, so I had to write one myself.
Here is my solution:
fun getAllFilesInResources()
{
val projectDirAbsolutePath = Paths.get("").toAbsolutePath().toString()
val resourcesPath = Paths.get(projectDirAbsolutePath, "/src/main/resources")
val paths = Files.walk(resourcesPath)
.filter { item -> Files.isRegularFile(item) }
.filter { item -> item.toString().endsWith(".txt") }
.forEach { item -> println("filename: $item") }
}
Here I have parsed through all the files in the /src/main/resources folder and then filtered only the regular files (no directories included) and then filtered for the text files within the resources directory.
The output is a list of all the absolute file paths with the extension .txt in the resources folder. Now you can use these paths to copy the files to an external folder.

There are no methods for it (i.e. Application::class.java.listFilesInDirectory("/folder/")), but you can create your own system to list the files in a directory:
#Throws(IOException::class)
fun getResourceFiles(path: String): List<String> = getResourceAsStream(path).use{
return if(it == null) emptyList()
else BufferedReader(InputStreamReader(it)).readLines()
}
private fun getResourceAsStream(resource: String): InputStream? =
Thread.currentThread().contextClassLoader.getResourceAsStream(resource)
?: resource::class.java.getResourceAsStream(resource)
Then just call getResourceFiles("/folder/") and you'll get a list of files in the folder, assuming it's in the classpath.
This works because Kotlin has an extension function that reads lines into a List of Strings. The declaration is:
/**
* Reads this reader content as a list of lines.
*
* Do not use this function for huge files.
*/
public fun Reader.readLines(): List<String> {
val result = arrayListOf<String>()
forEachLine { result.add(it) }
return result
}

Two distinct parts:
Obtain a file that represents the resource directory
Traverse the directory
For 1 you can use Java's getResource:
val dir = File( object {}.javaClass.getResource(directoryPath).file )
For 2 you can use Kotlin's File.walk extension function that returns a sequence of files which you can process, e.g:
dir.walk().forEach { f ->
if(f.isFile) {
println("file ${f.name}")
} else {
println("dir ${f.name}")
}
}
Put together you may end up with the following code:
fun onEachResource(path: String, action: (File) -> Unit) {
fun resource2file(path: String): File {
val resourceURL = object {}.javaClass.getResource(path)
return File(checkNotNull(resourceURL, { "Path not found: '$path'" }).file)
}
with(resource2file(path)) {
this.walk().forEach { f -> action(f) }
}
}
so that if you have resources/nested direcory, you can:
fun main() {
val print = { f: File ->
when (f.isFile) {
true -> println("[F] ${f.absolutePath}")
false -> println("[D] ${f.absolutePath}")
}
}
onEachResource("/nested", print)
}

Here is a solution to iterate over JAR-packed resources on JVM:
fun iterateResources(resourceDir: String) {
val resource = MethodHandles.lookup().lookupClass().classLoader.getResource(resourceDir)
?: error("Resource $resourceDir was not found")
FileSystems.newFileSystem(resource.toURI(), emptyMap<String, String>()).use { fs ->
Files.walk(fs.getPath(resourceDir))
.filter { it.extension == "ttf" }
.forEach { file -> println(file.toUri().toString()) }
}
}

Here is a pretty simple solution that I found.
File("/path/to/file.txt")
// make an iterable file tree
.walk()
// only files no directories
.filter { it.isFile }
// last modified from top to bottom (most recent on top)
.sortedByDescending { it.lastModified() }
// do things on the files
.forEachIndexed {
i, it ->
// use the most recent file and delete the other ones
if (i == 0) {
useMe(it)
} else {
it.delete()
}
}

Related

How to invoke function based on condition of iterated value of Mono<List<String>> without using subscribe()?

I want to invoke a function that will notify the admin about some information missing, but I do not want to subscribe to this Mono, because I will subscribe to it later. The problem is I have some log which is called inside doOnSuccess() and when I use subscribe() and then build a response where I zip listOfWords value, the same log is logged twice and I do not want a code to behave that way.
Is there any way to retrieve that value in checkCondition() in a way that will not invoke doOnSuccess() or should I use some other function in merge() that can replace doOnSuccess()?
Should I use subscribe() only once on given Mono or is it allowed to use it multiple times?
Thank you in advance!
The functions are called in the presented order.
Code where log is called:
private fun merge(list1: Mono<List<String>>, list2: Mono<List<String>>) =
Flux.merge(
list1.flatMapMany { Flux.fromIterable(it) },
list2.flatMapMany { Flux.fromIterable(it) }
)
.collectList()
.doOnSuccess { LOG.debug("List of words: $it") }
Code where subscribe is called:
private fun checkCondition(
listOfWords: Mono<List<String>>,
) {
listOfWords.subscribe {
it.forEach { word ->
if (someCondition(word)) {
alarmSystem.notify("Something is missing for word {0}")
}
}
}
}
Code where response is built:
private fun buildResponse(
map: Mono<Map<String, String>>,
list1: List<SomeObject>,
listOfWords: Mono<List<String>>
): Mono<List<Answer>> {
val response = Mono.zip(map, Mono.just(list1), listOfWords)
.map { tuple ->
run {
val tupleMap = tuple.t1
val list = tuple.t2
val words = tuple.t3
list
.filter { someCondition(words) }
.map { obj -> NewObject(x,y) }
}
}

getting error Missing calls inside every { ... } block in writing unit test cases in kotlin + Mockk + Junit5

the function I am testing,
class FileUtility {
companion object {
#JvmStatic
fun deleteFile(filePath: String) {
try {
val file = getFileObject(filePath)
file.delete()
} catch (ex :Exception) {
log.error("Exception while deleting the file", ex)
}
}
}
}
Unit test,
#Test
fun deleteFileTest() {
val filePath = "filePath"
val file = mockk<File>()
every { getFileObject(filePath) } returns file
deleteFile(filePath)
verify { file.delete() }
}
getting the following error on running this test case
io.mockk.MockKException: Missing calls inside every { ... } block.
is this any bug or am I writing wrong test case?
Assuming getFileObject is a top level function in FileUtility.kt file, you need to mock module wide functions with mockkStatic(...) with argument as the module’s class name.
For example “pkg.FileKt” for module File.kt in the pkg package.
#Test
fun deleteFileTest() {
val file = mockk<File>()
mockkStatic("pkg.FileUtilityKt")
val filePath = "filePath"
every { getFileObject(filePath) } returns file
every {file.delete()} answers {true}
deleteFile(filePath)
verify { file.delete() }
}

Reduce/Collect `List<Map<String, Set<String>` to `Map<String, Set<String>>`

After performing a parallelStream() on a List, I end up with a List<Map<String, Set<String>. I want to unify this into a Map<String, Set<String>> (which will only keep uniques across the List of Maps).
I am unfamiliar with the collect and reduce functions, so don't have anything to go ahead with.
Existing code:
private val TYPES = listOf("string", "integer")
private fun getLinesOfEachTypeAcrossMultipleFiles(files: List<File>): Map<String, Set<String>> {
return files
.parallelStream()
.map { file ->
TYPES.associate {
it to getRelevantTypeLinesFromFile(file)
}
}
// Converted into a Stream<String, Set<String>>
// .reduce() / collect() ?
}
private fun getRelevantTypeLinesFromFile(it: File): Set<String> {
// Sample code
return setOf()
}
If you're looking for an equivalent Java code, you can stream all the entries using flatMap and then collect them as a Map with a merge function as :
Map<String, Set<String>> some(List<Map<String, Set<String>>> listOfMap) {
return listOfMap.stream()
.flatMap(a -> a.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
(s1, s2) -> {
s1.addAll(s2);
return s1;
}));
}
I figured out and implemented a Kotlin-specific solution of using the fold operator (instead of reduce or collect):
private val TYPES = listOf("string", "integer")
private fun getLinesOfEachTypeAcrossMultipleFiles(files: List<File>): Map<String, Set<String>> {
return files
.map { file ->
TYPES.associate { it to getRelevantTypeLinesFromFile(file) }
}
.fold(mutableMapOf<String, MutableSet<String>>()) { acc, map ->
acc.apply {
map.forEach { key, value ->
acc.getOrPut(key) { mutableSetOf() }.addAll(value)
}
}
}
}
private fun getRelevantTypeLinesFromFile(it: File): Set<String> {
// Sample code
return setOf()
}
A benefit of using fold is that we don't need to change the type of the data from Map to MutableMap and Set to MutableSet.

Node hierarchy from directory path list

Hi I'm trying to build a node hierarchy based on the directory structure:
/first/
/first/second
/first/third
/first/third/forth
/first/third/fifth
/first/sixth
/first/sixth/seventh
/first/eighth
/first/ninth
I'm trying to get a node hierarchy similar to this:
first
second
third
forth
fifth
sixth
seventh
eighth
ninth
I'm using Kotlin for this, I'm still relatively new to Java and Kotlin so bear with me.
Note: I'm using FileTreeWalk to get the directories
fun getDirs(directoryName: String): MutableList<String> {
val ret = mutableListOf<String>()
File(directoryName).walk().forEach {
if (it.isDirectory) { ret.add(it.toString()) }
}
return ret
}
Right now all I have is this (Generates a flat hierarchy):
private fun nodesFromPathList(dirPaths: MutableList<String>) : Tree.Node {
val ret = Tree.Node("root")
for (dir in dirPaths) {
ret.add(Tree.Node(dir))
}
return ret
}
Any ideas?
I decided to put all the nodes into a map and connect the nodes together.
/**
* Creates a node hierarchy from list of paths
*/
private fun nodesFromPathList(dirPaths: MutableList<String>) : Tree.Node? {
var ret : Tree.Node? = null
val nodeMap = mutableMapOf<String, Tree.Node>()
// Add a node for each directory path and put it into a map
for (dir in dirPaths) {
val newNode = Tree.Node(nodeName(dir), skin)
if (ret == null)
ret = newNode
nodeMap.put(dir, newNode)
}
// Go through each one and add the child
nodeMap.forEach {
val parent = parentPath(it.key)
try {
nodeMap[parent]!!.add(it.value)
} catch (e: NullPointerException) {
println("Parent not found")
}
}
return ret
}
/**
* Returns current path
* "D:\dir\to\apath" ==> "apath"
*/
fun nodeName(path: String): String {
return path.split("\\").last()
}
/**
* Returns the parent path
* D:\dir\to\apath ==> D:\dir\to
*/
fun parentPath(path: String): String {
val split = path.trim('\\').split("\\")
var ret = ""
for (i in 0..split.size-2) {
ret += split[i] + "\\"
}
return ret.trim('\\')
}
I put the path into the key, and the tree node into the value. Iterated through the map and connected the children based on the parent. Example Map.:
[D:\android\assets=Tree$Node#56eb1af5,
D:\android\assets\Assets\abc=Tree$Node#48e3456d,
D:\android\assets\Assets\abc\bcd=Tree$Node#3e532818,
D:\android\assets\Assets\abc\cde=Tree$Node#16b07083]
I would find the parent from the key (by splitting the string), then set the parent.

How do I write to a file in Kotlin?

I can't seem to find this question yet, but what is the simplest, most-idiomatic way of opening/creating a file, writing to it, and then closing it? Looking at the kotlin.io reference and the Java documentation I managed to get this:
fun write() {
val writer = PrintWriter("file.txt") // java.io.PrintWriter
for ((member, originalInput) in history) { // history: Map<Member, String>
writer.append("$member, $originalInput\n")
}
writer.close()
}
This works, but I was wondering if there was a "proper" Kotlin way of doing this?
A bit more idiomatic. For PrintWriter, this example:
File("somefile.txt").printWriter().use { out ->
history.forEach {
out.println("${it.key}, ${it.value}")
}
}
The for loop, or forEach depends on your style. No reason to use append(x) since that is basically write(x.toString()) and you already give it a string. And println(x) basically does write(x) after converting a null to "null". And println() does the correct line ending.
If you are using data classes of Kotlin, they can already be output because they have a nice toString() method already.
Also, in this case if you wanted to use BufferedWriter it would produce the same results:
File("somefile.txt").bufferedWriter().use { out ->
history.forEach {
out.write("${it.key}, ${it.value}\n")
}
}
Also you can use out.newLine() instead of \n if you want it to be correct for the current operating system in which it is running. And if you were doing that all the time, you would likely create an extension function:
fun BufferedWriter.writeLn(line: String) {
this.write(line)
this.newLine()
}
And then use that instead:
File("somefile.txt").bufferedWriter().use { out ->
history.forEach {
out.writeLn("${it.key}, ${it.value}")
}
}
And that's how Kotlin rolls. Change things in API's to make them how you want them to be.
Wildly different flavours for this are in another answer: https://stackoverflow.com/a/35462184/3679676
Other fun variations so you can see the power of Kotlin:
A quick version by creating the string to write all at once:
File("somefile.txt").writeText(history.entries.joinToString("\n") { "${it.key}, ${it.value}" })
// or just use the toString() method without transform:
File("somefile.txt").writeText(x.entries.joinToString("\n"))
Or assuming you might do other functional things like filter lines or take only the first 100, etc. You could go this route:
File("somefile.txt").printWriter().use { out ->
history.map { "${it.key}, ${it.value}" }
.filter { ... }
.take(100)
.forEach { out.println(it) }
}
Or given an Iterable, allow writing it to a file using a transform to a string, by creating extension functions (similar to writeText() version above, but streams the content instead of materializing a big string first):
fun <T: Any> Iterable<T>.toFile(output: File, transform: (T)->String = {it.toString()}) {
output.bufferedWriter().use { out ->
this.map(transform).forEach { out.write(it); out.newLine() }
}
}
fun <T: Any> Iterable<T>.toFile(outputFilename: String, transform: (T)->String = {it.toString()}) {
this.toFile(File(outputFilename), transform)
}
used as any of these:
history.entries.toFile(File("somefile.txt")) { "${it.key}, ${it.value}" }
history.entries.toFile("somefile.txt") { "${it.key}, ${it.value}" }
or use default toString() on each item:
history.entries.toFile(File("somefile.txt"))
history.entries.toFile("somefile.txt")
Or given a File, allow filling it from an Iterable, by creating this extension function:
fun <T: Any> File.fillWith(things: Iterable<T>, transform: (T)->String = {it.toString()}) {
this.bufferedWriter().use { out ->
things.map(transform).forEach { out.write(it); out.newLine() }
}
}
with usage of:
File("somefile.txt").fillWith(history.entries) { "${it.key}, ${it.value}" }
or use default toString() on each item:
File("somefile.txt").fillWith(history.entries)
which if you had the other toFile extension already, you could rewrite having one extension call the other:
fun <T: Any> File.fillWith(things: Iterable<T>, transform: (T)->String = {it.toString()}) {
things.toFile(this, transform)
}
It mostly looks ok to me. The only thing different I would do is use the "use" extension defined in ReadWrite to auto close the writer.
PrintWriter("file.txt").use {
for ((member, originalInput) in history) { // history: Map<Member, String>
it.append("$member, $originalInput\n")
}
}
At the very minimum, you could use:
FileWriter(filename).use { it.write(text) }
FileWriter is a convenience class for writing character files (provided by Java, and hence available in Kotlin). It extends Closeable, and hence can be used by Kotlin's ".use" extension method.
The .use extension method automatically closes the calling object once the block exits, thus providing an idiomatic way to close the file after it's written.
Some Kotlin magic allows to omit referencing the stream on each read or write call:
fun <T : Closeable, R> T.useWith(block: T.() -> R): R = use { with(it, block) }
File("a.in").bufferedReader().useWith {
File("a.out").printWriter().useWith {
val (a, b) = readLine()!!.split(' ').map(String::toInt)
println(a + b)
}
}
Scanner(File("b.in")).useWith {
PrintWriter("b.out").useWith {
val a = nextInt()
val b = nextInt()
println(a + b)
}
}
try{
val fileWriter = FileWriter("test.txt", true)
fileWriter.write(string+ "\n")
fileWriter.close()
} catch (exception: Exception){
println(exception.message)
}
Example as easy
val path = context!!.filesDir.absolutePath // => /data/user/0/com.example.test/files
File("$path/filename.txt").writeText("hello")
File(requireContext().filesDir, "TodayTaskListChange.txt").writeText("write your test here...")