如何在Kotlin中获取当前工作目录?

huangapple go评论76阅读模式
英文:

How to get the current working directory in Kotlin?

问题

我需要知道使用Kotlin编写的我的简单应用程序的当前工作目录的完整路径。

英文:

I need to know the full path of my current working directory of my simple application using Kotlin.

答案1

得分: 20

这将提供应用程序运行位置的完整绝对路径。

import java.nio.file.Paths;

val path = Paths.get("").toAbsolutePath().toString();
英文:

This will provide the full absolute path from where application will run.

import java.nio.file.Paths

val path = Paths.get("").toAbsolutePath().toString()       

答案2

得分: 6

你还可以从 System 获取它。

val path = System.getProperty("user.dir")
英文:

You can also get it from System.

val path = System.getProperty("user.dir")

答案3

得分: 0

如果您正在使用 Kotlin 开发 Android 应用,请尝试以下代码:

Kotlin

applicationInfo.dataDir

如果您正在使用 Java 开发 Android 应用:

getApplicationInfo().dataDir

该代码会返回一个字符串。

英文:

If you are working with kotlin on android try with:

Kotlin

applicationInfo.dataDir

if you are working on android using Java.

getApplicationInfo().dataDir

it returns a String.

huangapple
  • 本文由 发表于 2020年10月2日 19:54:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/64171104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定