Android 将目录转换为Kotlin中的对象
我正试图以这种方式解析一些文件:Android 将目录转换为Kotlin中的对象,android,kotlin,Android,Kotlin,我正试图以这种方式解析一些文件: File(tentConfig.getPathRepository()) .walkTopDown() .forEach { file -> processFile(file) } 此文件的路径为:/communications/email/begginer/.file 我必须将该路径转换为如下对象: 我的通信应该是我的类别,电子邮件应该是通信的子类别,初学者电子邮件的子类别 我的process方法负责将此
File(tentConfig.getPathRepository())
.walkTopDown()
.forEach { file -> processFile(file) }
此文件的路径为:/communications/email/begginer/.file
我必须将该路径转换为如下对象:
我的通信应该是我的类别,电子邮件应该是通信的子类别,初学者电子邮件的子类别
我的process方法负责将此路径序列化到对象,但我确信有更好的解决方案
private fun processCategory(currentFile: File) {
val listOfDirectory = currentFile.path.split("/".toRegex())
listOfDirectory.forEachIndexed { index, folderName ->
if (index == 0) {
val currentCategory = parseYmlFile(currentFile, Category::class)
lesson.categories.forEach { itCategory ->
if (itCategory.title != currentCategory.title) lesson.categories.add(currentCategory)
}
} else {
val subCategory = parseYmlFile(currentFile, Category::class)
lesson.categories[subCategory.index].subcategories.add(subCategory)
}
}
}
出于演示/测试的目的,我对Category的实现可能与您的不同。这是我使用的一个:
inner class Category(val s: String, var subCategory: Category? = null)
话虽如此,这里有一个小函数,它将循环给定文件的路径,并构建一个类别层次结构,将每个元素按正确的顺序排列:
private fun processCategory(currentFile: File): Category? {
val listOfDirectory = currentFile.path.split("/".toRegex())
//The root category (in your example, communications)
var rootCategory: Category? = null
//A reminder of the current Category, so we can attach the next one to it
var currentCategory: Category? = null
listOfDirectory.forEach {
if (rootCategory == null) {
//First element, so I need to create the root category
rootCategory = Category(it)
currentCategory = rootCategory
} else {
//Other elements are simply created
val nextCategory = Category(it)
//Added as a subCategory of the previous category
currentCategory!!.subCategory = nextCategory
//And we progress within the chain
currentCategory = nextCategory
}
}
//In the end, my root category will contain :
// Category("communications", Category("email", Category("Beginner", null)))
return rootCategory
}
为了演示/测试的目的,您可以将我正在使用的构造函数替换为您的YmlParser,从而使这段代码适应您的需要,我的Category实现可能与您的有所不同。这是我使用的一个:
inner class Category(val s: String, var subCategory: Category? = null)
话虽如此,这里有一个小函数,它将循环给定文件的路径,并构建一个类别层次结构,将每个元素按正确的顺序排列:
private fun processCategory(currentFile: File): Category? {
val listOfDirectory = currentFile.path.split("/".toRegex())
//The root category (in your example, communications)
var rootCategory: Category? = null
//A reminder of the current Category, so we can attach the next one to it
var currentCategory: Category? = null
listOfDirectory.forEach {
if (rootCategory == null) {
//First element, so I need to create the root category
rootCategory = Category(it)
currentCategory = rootCategory
} else {
//Other elements are simply created
val nextCategory = Category(it)
//Added as a subCategory of the previous category
currentCategory!!.subCategory = nextCategory
//And we progress within the chain
currentCategory = nextCategory
}
}
//In the end, my root category will contain :
// Category("communications", Category("email", Category("Beginner", null)))
return rootCategory
}
通过将我正在使用的构造函数替换为您的YmlParser,您当然可以根据您的需要调整此代码问题出在哪里?您的解决方案缺少什么?@ESalaIt无法正确转换我的电子邮件子类别以进行通信,而初学者则无法正确转换电子邮件。您的路径是否始终包含1个文件夹和2个子文件夹?@NSimon否,它应该是N个文件夹,如AA/B/CC/F转换此表单类别(AA)。添加子类别(B)。添加子类别(CC).addSubCategory(F)问题是什么?您的解决方案缺少什么?@ESalaIt无法正确转换我的电子邮件子类别以进行通信,而初学者则无法正确转换电子邮件。您的路径是否始终包含1个文件夹和2个子文件夹?@NSimon否,它应该是N个文件夹,如AA/B/CC/F转换此表单类别(AA)。添加子类别(B)。添加子类别(CC).addSubCategory(F)