如何在Context.MODE_PRIVATE中创建嵌套的文件夹和文件? [英] How to create nested folders and file in Context.MODE_PRIVATE?
问题描述
我有一个要求,文件和文件夹必须使用Context.MODE_PRIVATE
以嵌套结构编写.
I have a requirement where files and folders are required to be written in nested structure using Context.MODE_PRIVATE
.
我发现我们可以使用openFileOutput(FILENAME, Context.MODE_PRIVATE);
创建文件,并且能够使用此方法创建文件.但是后来,我发现使用此方法无法创建嵌套文件结构,即用于
I found that we can create file using openFileOutput(FILENAME, Context.MODE_PRIVATE);
and was able to create files using this method.But later on I found that using this method I can not create a nested file structure i.e for
openFileOutput("foo/myText.txt", Context.MODE_PRIVATE);
抛出异常:
java.lang.IllegalArgumentException: File foo/myText.txt contains a path separator
可能是因为它只期望fileName & not a filepath
并且文档中还说
May be because it expects only a fileName & not a filepath
and also documentation says
打开与此Context的应用程序包关联的私有文件 用于写作.
Open a private file associated with this Context's application package for writing.
然后我发现getFilesDir()
返回了Context的应用程序包,我可以在其中使用普通的Java File I/O创建文件.但是我不知道创建它的权限,即它是否为MODE_APPEND
,MODE_PRIVATE
,MODE_WORLD_READABLE
或MODE_WORLD_WRITEABLE
.
Then I found that getFilesDir()
returns Context's application package where I can create files using the normal Java File I/O.But I don't know the permissions under which it is created i.e whether it is MODE_APPEND
,MODE_PRIVATE
,MODE_WORLD_READABLE
or MODE_WORLD_WRITEABLE
.
所以,我有以下问题:-
So, I have following questions:-
-
如何在MODE_PRIVATE下使用openFileOutput()方法创建嵌套文件结构?
How do I create nested file structure using openFileOutput() method under MODE_PRIVATE(if it's possible)?
如果openFileOutput()不适用于创建嵌套结构,那么可以安全地使用getFilesDir()
和Java File I/O组合满足我的要求吗?用这种方式创建的文件有什么权限?
If openFileOutput() is not applicable to create a nested structure,then is it safe to use getFilesDir()
and Java File I/O combination for my requirement? What is the permission for the files created using this way?
推荐答案
您需要先创建foo
目录,然后在该目录中创建文件.
You need to create foo
directory first then create file inside that dir.
使用 getDir(String name,int mode)将目录创建到内部存储器中.方法检索,如果需要,创建一个新目录,应用程序可以在该目录中放置自己的自定义数据文件.您可以使用返回的File对象在此目录中创建和访问文件.
Use getDir(String name, int mode) to create directory into internal memory. The method 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.
例如
// Create directory into internal memory;
File mydir = context.getDir("mydir", Context.MODE_PRIVATE);
// Get a file myfile within the dir mydir.
File fileWithinMyDir = new File(mydir, "myfile");
// Use the stream as usual to write into the file.
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
所以您需要将代码编写为
// Create foo directory into internal memory;
File mydir = getDir("foo", Context.MODE_PRIVATE);
// Get a file myText.txt within the dir foo.
File fileWithinMyDir = new File(mydir, "myText.txt");
// Use the stream as usual to write into the file.
try {
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
将foo/myText.txt创建到内部存储中.
对于嵌套目录,应使用常规的java方法.像
For nested directories, you should use normal java method. Like
new File(parentDir, "childDir").mkdir();
因此更新后的示例应该是
So updated example should be
// Create directory into internal memory;
File mydir = getDir("mydir", Context.MODE_PRIVATE);
// Create sub-directory mysubdir
File mySubDir = new File(mydir, "mysubdir");
mySubDir.mkdir();
// Get a file myfile within the dir mySubDir.
File fileWithinMyDir = new File(mySubDir, "myfile");
// Use the stream as usual to write into the file.
FileOutputStream out = new FileOutputStream(fileWithinMyDir);
这篇关于如何在Context.MODE_PRIVATE中创建嵌套的文件夹和文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!