-1
private object Bot {
    val token = File("token.txt").reader().readLines()

    fun setup() {

    }
}

fun main(args: Array<String>) {
    print(Bot.token)
}

Even though the token.txt file is in the same directory as the Bot.kt file it throws a java.io.FileNotFoundException.

paspielka
  • 47
  • 5
  • 2
    Does this answer your question? [Java Text File I/O Location](https://stackoverflow.com/questions/14735575/java-text-file-i-o-location) – Vlam Sep 26 '22 at 11:28
  • val token = File("token.txt").absolutePath.reader().readLines() I tried doing it like this, but it only prints the path for some reason not the lines. – paspielka Sep 26 '22 at 11:43

1 Answers1

1

If you only specify "token.txt" it will refer to the root directory of the project not to the directory of the class that is calling. Here you can see how you can specify paths (example is in java but that is the library that kotlin JVM is using)

So in your case if you want to keep file next to the class it will probably look something like

File("src/main/kotlin/token.txt")

Or whatever your file structure looks like

Edit to explain your comment File("token.txt").absolutePath will return the path as the string and calling reader().readLines() on that will just read that returned string(path) not the contents

Vojin Purić
  • 2,140
  • 7
  • 9
  • 22
  • 2
    *it will refer to the root directory of the project* - not really. It will refer to the working directory of the java application process. If you compile the app and run it somewhere from the compiled jar, the `token.txt` path will be resolved against the current directory (the place from which you run your app). It is unrelated to the project sources. – Joffrey Sep 26 '22 at 11:57
  • @Joffrey Good point, you can edit my post to include that and I will accept. Or I will if you don't want to bother – Vojin Purić Sep 26 '22 at 12:01
  • 2
    Or just point to [my previous answer](/a/73802855/10134209)… – gidds Sep 26 '22 at 15:23