gpt4 book ai didi

java - 创建文件夹

转载 作者:行者123 更新时间:2023-12-02 11:28:05 25 4
gpt4 key购买 nike

我正在尝试创建一个名为“YouDown”的文件夹,目前我不关心该文件夹位于何处,但此时我只想弄清楚如何创建它。我发现我的第一个问题是 mkdir()mkdirs() 由于不知道它是一个 boolean 值而被忽略。我创建了 success 的 boolean 值,现在它不会被忽略。接下来,我创建了检测到创建已存在的每个步骤的 log.d。它注册它“不存在”“正在创建”,然后“已创建”“创建失败”。它跳转到“创建失败”。我现在发现的所有有帮助的内容都只是重复我过去几天读过的内容。我还在研究如何将其应用到特定的路径方式,例如我希望在目录 Music 文件夹中创建它的变量字符串

//最新尝试

String Tag2 = "YouDown"

if (!dir.exists()) {
Log.d(Tag2,"Doesnt Exist");
boolean success = false;

try{
success = dir.mkdir();
Log.d(Tag2,"Being Created");

}
catch(SecurityException se){
//handle it
}
if(success) {
Log.d(Tag2, "Created");
} else{
Log.d(Tag2, "Creation Failed");
}
}

//其他尝试

String path = "/sdcard/Music/Youdown"

if(new File(path).exists()){

Log.d(Tag2, "Exists");

} else {

Log.d(Tag2, "Being Created");
Boolean succes = new File(path).mkdir();

if(success){

Log.d(Tag2, "Created"

} else {

Log.d(Tag2, "Failed"
}

最新尝试

File dir = new File(Environment.getExternalStorageDirectory(), path2);
Boolean A = dir.mkdirs();

if(A){
Log.d(Tag2,"Created");
}
if(!A){
Log.d(Tag2,"Failed");
}

最佳答案

尽管 Android 具有分层文件系统,但您的应用程序只能在某些位置读取和写入。首先,我建议使用 android.content.ContextgetDir() 方法。正如文档所述:

Retrieve, creating if needed, a new directory in which the application can place its own custom data files. You can use the returned File object to create and access files in this directory. Note that files created through a File object will only be accessible by your own application; you can only set the mode of the entire directory, not of individual files.

如果要访问共享目录,则需要调用其他方法,例如Context.getExternalFilesDir()

关于java - 创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49475398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com