Issues with handling files | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Issues with handling files

So what I want to do is for each file in a directory use it to create an object and store that object into a collection of some sort. I also need each object to remember its original name, but that's easy enough. Here's the first little bit fun main(args: Array<String>){ val dir = File(args.joinToString()) Now I've tried 2 routes. dir.walk().forEach or files=dir.listFiles() I think either of these would work, but my bigger problem is that I can't store them anywhere. Id like each File to be it's own variable but the number of files is variable and I'm pretty sure kotlin doesn't support dynamically named variables. And I'm having trouble using an array or map because I need to initialize it but I'm not sure how. Short version. I need help taking n files from a directory, applying each to a class constructor, and then store those objects in some n element long collection

31st Mar 2023, 9:20 PM
Bob
3 Answers
+ 5
Why not try a list? You can add each item without needing to know how many there will be. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/
31st Mar 2023, 9:40 PM
Ausgrindtube
Ausgrindtube - avatar
+ 1
You're welcome. Yep, I can understand that. That's the problem of search engines now, they search what they think you mean and not exactly what you mean and that often leads us down the completely wrong path. Happy coding!
31st Mar 2023, 10:06 PM
Ausgrindtube
Ausgrindtube - avatar
0
Thank you. I was looking for this kind of list but for whatever reason when I searched list on the kotlin site all I found was the arrayList
31st Mar 2023, 9:48 PM
Bob