Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Kotlin中的.txt文件中选择随机行_Kotlin_Random - Fatal编程技术网

如何从Kotlin中的.txt文件中选择随机行

如何从Kotlin中的.txt文件中选择随机行,kotlin,random,Kotlin,Random,我想创建一个程序,从.txt文件中随机打印出一行。这就是我目前所处的位置,我能找到的其他类似问题只有其他语言。例如,Python使用random.choice()操作,我在这个问题中发现: 谢谢大家抽出时间 fun main() { val file = "text.txt" println(file.random("text.txt")) //This code doesn't work, I'm just illustrating wh

我想创建一个程序,从.txt文件中随机打印出一行。这就是我目前所处的位置,我能找到的其他类似问题只有其他语言。例如,Python使用
random.choice()
操作,我在这个问题中发现:

谢谢大家抽出时间

fun main() {
    val file = "text.txt"
    println(file.random("text.txt")) //This code doesn't work, I'm just illustrating what I was looking to do.
}
我觉得有必要进行编辑: 我正在导入的库

import java.io.FileReader
import kotlin.system.exitProcess
import java.io.FileWriter
import kotlin.random.Random

我学到了更多的东西:

有一个函数
RandomAccessFile
,用于我想做的事情,但是,我没有找到在Kotlin中如何使用它的好来源

编辑评论: 我可以从文件中读取,当我这样做时,所有的行都会按顺序打印

1:我知道如何生成随机数,但是我不知道如何将其添加到.txt文件中

2:我试图使用下面的代码来添加一个对应于它所在行的数字,但是,这段代码给了我一个错误,因为在运行时I变量没有被理解为现有的数字

贴纸箱下方和贴纸箱中的错误代码,用于整洁。

fun main(){
变量i=1
println(“请输入一个值,完成时键入DONE,读取以打印。”)
val循环=0
while(循环<1){
var response=readLine()
如果(响应=“完成”){
退出进程(0)
}否则如果(响应=“读取”){
随机阅读()
}否则{
WriteToFile(i+响应)
i+1
}
}
}
错误:(23,31)Kotlin:无法使用提供的参数调用以下函数:

公共最终运算符fun plus(其他:字节):在kotlin.Int中定义的Int

公共最终运算符fun plus(其他:Double):在kotlin.Int中定义的Double

公共最终运算符fun plus(其他:Float):在kotlin.Int中定义的Float

公共最终运算符fun plus(其他:Int):在kotlin.Int中定义的Int

公共最终运算符fun plus(其他:Long):在kotlin.Int中定义的Long

公共最终运算符fun plus(其他:短):在kotlin.Int中定义的Int

我还试图:

fun main() {
    println("Please input a value, type DONE when done, READ to print.")
    val loop = 0
    while (loop < 1) {
        var response = readLine()
        if (response == "DONE") {
            exitProcess(0)
        }else if (response== "READ") {
            RandomRead()
        } else {
                WriteToFile(response)
        }
    }
}
fun WriteToFile(str: String?) {
    var i = 0
    try {
        var fo=FileWriter("test.txt")
        fo.write(i + " " + str + "\n")
        fo.close()
        i+1
    }catch (ex:Exception){
        println(ex.message)
    }
}
fun main(){
println(“请输入一个值,完成时键入DONE,读取以打印。”)
val循环=0
while(循环<1){
var response=readLine()
如果(响应=“完成”){
退出进程(0)
}否则如果(响应=“读取”){
随机阅读()
}否则{
写入文件(响应)
}
}
}
有趣的WriteToFile(str:String?){
变量i=0
试一试{
var fo=FileWriter(“test.txt”)
fo.write(i+“”+str+“\n”)
fo.close()
i+1
}捕获(例如:异常){
println(例如消息)
}
}
错误:(37,20)Kotlin:无法使用提供的参数调用以下函数:

公共最终运算符fun plus(其他:字节):在kotlin.Int中定义的Int

公共最终运算符fun plus(其他:Double):在kotlin.Int中定义的Double

公共最终运算符fun plus(其他:Float):在kotlin.Int中定义的Float

公共最终运算符fun plus(其他:Int):在kotlin.Int中定义的Int

公共最终运算符fun plus(其他:Long):在kotlin.Int中定义的Long


公共最终操作员fun plus(其他:简称):如注释中所述,在kotlin.Int中定义的Int,与以下内容结合使用:

文件(“File.txt”).readLines().random()
但是,正如
readLines()
的文档所述:

不要将此功能用于大型文件


你能逐行读这个文本文件吗?您知道如何生成随机整数吗?请查看
文件
对象的
readLines
扩展函数,该函数提供了一个
列表
。然后你只需要选择一条随机的线。
fun main() {
    println("Please input a value, type DONE when done, READ to print.")
    val loop = 0
    while (loop < 1) {
        var response = readLine()
        if (response == "DONE") {
            exitProcess(0)
        }else if (response== "READ") {
            RandomRead()
        } else {
                WriteToFile(response)
        }
    }
}
fun WriteToFile(str: String?) {
    var i = 0
    try {
        var fo=FileWriter("test.txt")
        fo.write(i + " " + str + "\n")
        fo.close()
        i+1
    }catch (ex:Exception){
        println(ex.message)
    }
}