var filename = "blesson.txt"
var wallpaperDirectory = File("/sdcard/Wallpaper")
 wallpaperDirectory.mkdirs()
val outputFile = File(wallpaperDirectory, filename)
val fos = FileOutputStream(outputFile)

我正在try 使用Kotlin在Android设备上创建一个新目录,但功能mkdirs()不起作用.

var filename = "blesson.txt"
var wallpaperDirectory = File(Environment.getExternalStorageDirectory().absolutePath)//("/sdcard/Wallpaper")
wall
val outputFile = File(wallpaperDirectory, filename)
val fos = FileOutputStream(outputFile)

我也试过了,它并没有创建一个新的目录

推荐答案

这在Kotlin上非常有效

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    var filename = "blesson.txt"
    // create a File object for the parent directory
    val wallpaperDirectory = File("/sdcard/Wallpaper/")
    // have the object build the directory structure, if needed.
    wallpaperDirectory.mkdirs()
    // create a File object for the output file
    val outputFile = File(wallpaperDirectory, filename)
    // now attach the OutputStream to the file object, instead of a String representation
    try {
      val fos = FileOutputStream(outputFile)
    } catch (e: FileNotFoundException) {
      e.printStackTrace()
    }

  }
}

Kotlin相关问答推荐

映射中列表类型的Kotlin可空接收器?

如何避免使用公共类实现内部接口

Scala与Kotlin中的迭代

Spring Boot kotlin协程不能并行运行

数据流弹性模板失败,出现错误&未知非复合转换urn";

Kotlin stlib中是否有用于将列表<;对<;A,B&>;转换为对<;列表<;A&>,列表<;B&>;的函数

S使用MAP和ElseThrow的习惯用法是什么?

如何更改默认推断没有接收者的函数类型?

垂直滚动条下拉菜单的桌面组合

如何避免键盘打开时jetpack compose 内容上升

kotlin 如何决定 lambda 中的参数名称?

Kotlin 条件格式字符串

如何通过 compose 处理剪切区域?

如何创建 Kotlin DSL - DSL 语法 Kotlin

如何使用 Kotlin Coroutines 使 setOnClickListener debounce 1 秒?

在Kotlin中为Android编写库会有开销吗?

未解决的参考 dagger 2 + kotlin + android gradle

如何在伴随对象中使用泛型

Android Jetpack Compose - 图像无法zoom 到框的宽度和高度

如何在 Fragment 中使用 Anko DSL?