Android在内存中创建文件夹问题

Android在内存中创建文件夹问题,android,memory,internal,Android,Memory,Internal,我对在内存中为我的应用程序创建文件夹有点问题。我正在使用这段代码: public static void createFoldersInInternalStorage(Context context){ try { File usersFolder = context.getDir("users", Context.MODE_PRIVATE); File fileWithinMyDir = new File(usersFolder, "users

我对在内存中为我的应用程序创建文件夹有点问题。我正在使用这段代码:

    public static void createFoldersInInternalStorage(Context context){
    try {

        File usersFolder = context.getDir("users", Context.MODE_PRIVATE);
        File fileWithinMyDir = new File(usersFolder, "users.txt"); //Getting a file within the dir.
        FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.

        File dataFolder = context.getDir("data", Context.MODE_PRIVATE);
        File fileWithinMyDir2 = new File(dataFolder, "data.txt"); //Getting a file within the dir.
        FileOutputStream out2 = new FileOutputStream(fileWithinMyDir2); //Use the stream as usual to write into the file.

        File publicFolder = context.getDir("public", Context.MODE_PRIVATE);
        File fileWithinMyDir3 = new File(publicFolder, "public.txt"); //Getting a file within the dir.
        FileOutputStream out3 = new FileOutputStream(fileWithinMyDir3); //Use the stream as usual to write into the file.

    } catch(FileNotFoundException e){
        e.printStackTrace();
    }
}
因此,文件夹被创建,但在其名称前面有一个开头
“app\u”
app\u用户
app\u数据
app\u公共
。有没有办法用我给的名字创建文件夹?还有另一个问题:我想首先创建文件夹
文档
,而不是所有其他文件夹
“数据、公共、用户”
。。。。最后一个问题是:如果我想在内存中的
Documents/Users/myfile.txt
中创建一个文件,我如何给出正确的文件夹路径

提前谢谢

有没有办法用我给的名字创建文件夹

使用
getfiledir()
和Java文件I/O代替
getDir()

如果我想在内存中的Documents/Users/myfile.txt中创建文件,如何提供正确的文件夹路径

使用
getFilesDir()
和Java文件I/O,例如
文件
构造函数,它采用
文件
字符串
来组合路径。

您可以使用以下方法:

File myDir = context.getFilesDir();
String filename = "documents/users/userId/imagename.png";
File file = new File(myDir, filename);
file.createNewFile();
file.mkdirs();
FileOutputStream fos = new FileOutputStream(file);
fos.write(mediaCardBuffer);
fos.flush();
fos.close();

我刚刚尝试了这个,但它没有创建文件或文件夹:
fileusersfolder=context.getFilesDir();File fileWithinMyDir=新文件(usersFolder,“documents/users/users.txt”)//获取目录中的文件。FileOutputStream out=新的FileOutputStream(fileWithinMyDir);out.write(“adasdad.getBytes())@夸夸其谈:当然。目录不存在。您必须创建目录。这是标准的JavaI/O,是学习Java时应该学习的内容。在指向最深目录的
文件
对象上使用
mkdirs()
,创建指向该点的目录树。在内存中创建文件夹时,深度是否有限制?因为现在我在
outputstream
之前使用上面的代码+
file.mkdirs()
,它在
outputstream
行上抛出一个
FileNotFoundException
。我试图创建的路径是
documents/users/userId/3456.png
@Bombastic:我所知道的
mkdirs()
没有限制。再次调用目录上的
mkdirs()
。如果在文件上调用它,它将创建一个带有文件名的目录,这意味着您无法创建具有相同名称的文件。看见