如何在android应用程序中创建特定文件夹的文件?

如何在android应用程序中创建特定文件夹的文件?,android,Android,在我的应用程序中,我想在缓存文件夹中创建一个文本文件,首先我要做的是在缓存目录中创建一个文件夹 File myDir = new File(getCacheDir(), "MySecretFolder"); myDir.mkdir(); 然后,我想在创建的文件夹中创建一个文本文件,使用下面的代码,它似乎不存在。相反,下面的代码在“files”文件夹中创建文本文件,该文件夹与“cache”文件夹位于同一目录中 所以我的问题是,如何正确地指定“MySecretFolder”来生成文本文件 我尝试了

在我的应用程序中,我想在缓存文件夹中创建一个文本文件,首先我要做的是在缓存目录中创建一个文件夹

File myDir = new File(getCacheDir(), "MySecretFolder");
myDir.mkdir();
然后,我想在创建的文件夹中创建一个文本文件,使用下面的代码,它似乎不存在。相反,下面的代码在“files”文件夹中创建文本文件,该文件夹与“cache”文件夹位于同一目录中

所以我的问题是,如何正确地指定“MySecretFolder”来生成文本文件

我尝试了以下方法:

“/data/data/com.example.myandroid.cuecards/cache/MySecretFolder”,但如果我尝试这样做,它会使我的整个应用程序崩溃。如何将文本文件正确保存在缓存/MySecretFolder中?

使用getCacheDir()。它返回文件系统上特定于应用程序的缓存目录的绝对路径。然后您可以创建目录

File myDir = new File(getCacheDir(), "folder");
myDir.mkdir();
请试试这个也许对你有帮助

好的,如果你想在特定的文件夹中创建文本文件,那么你可以尝试使用下面的代码

try {
        String rootPath = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/MyFolder/";
        File root = new File(rootPath);
        if (!root.exists()) {
            root.mkdirs();
        }
        File f = new File(rootPath + "mttext.txt");
        if (f.exists()) {
            f.delete();
        }
        f.createNewFile();

        FileOutputStream out = new FileOutputStream(f);

        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
换衣服

fOut = openFileOutput("secret.txt",MODE_PRIVATE);


这将使MySecretFolder下的secret.txt

getPrivateDir将在您的私人区域创建一个文件夹(Context.MODE\u WORLD\u可写-使用Context.MODE\u中适合您的内容)

如果文件目录中的私人文件夹中不存在文件,openPrivateFileInput将创建该文件,并返回FileInputStream:

/data/data/your.packagename/files 
您的应用程序专用文件夹位于中

/data/data/your.packagename 

public FileInputStream openPrivateFileInput(String name) throws  FileNotFoundException
{
    return context.openFileInput(name);
}
如果您的软件包名称为uno.due.com,则您的应用程序专用文件夹为:

/data/data/uno.due.com
下面的所有目录都是由您或android为您创建的天气。当您按上述方式创建文件时,它将位于:

/data/data/uno.due.com/files

创建文件夹、文件和写入/附加到文件的简单易用代码

try {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newfoldername/";    // it will return root directory of internal storage
        File root = new File(path);
        if (!root.exists()) {
            root.mkdirs();       // create folder if not exist 
        }
        File file = new File(rootPath + "log.txt");
        if (!file.exists()) {
            file.createNewFile();   // create file if not exist 
        }
        BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
        buf.append("hi this will write in to file");
        buf.newLine();  // pointer will be nextline
        buf.close();

    } 
 catch (Exception e) {
        e.printStackTrace();
    }
注意:它需要Android外部存储权限,所以在AndroidManifest.xml中添加以下行

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>


我想你还没有读过我的问题。我想学习如何在指定目录中创建文本文件。我已经知道如何创建目录..这将在外部存储中创建MyFolder。。但问题是要在缓存目录中添加文件夹…如果要在缓存目录中添加文件夹,请将根路径更改为rootpath=myDir.getAbsolutepath();正如我在上面创建的。我明白了,我们可以这样做,因为mydir已经读取了地址,如果它不存在,它只会创建文本文件。我认为这不是正确的方法,官方文档说你不能在openFileOutput函数内写路径分隔符,int)@NicolaGallazzi:同意你。。。我们不应该在
openFileOutput
参数中使用路径分隔符,谢谢更正
/data/data/uno.due.com/files
try {
        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/newfoldername/";    // it will return root directory of internal storage
        File root = new File(path);
        if (!root.exists()) {
            root.mkdirs();       // create folder if not exist 
        }
        File file = new File(rootPath + "log.txt");
        if (!file.exists()) {
            file.createNewFile();   // create file if not exist 
        }
        BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
        buf.append("hi this will write in to file");
        buf.newLine();  // pointer will be nextline
        buf.close();

    } 
 catch (Exception e) {
        e.printStackTrace();
    }
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>