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

时间:2015-08-31 05:40:57

标签: android

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

File myDir = new File(getCacheDir(), "MySecretFolder");
myDir.mkdir();

然后我想使用下面的代码在该创建的文件夹中创建一个文本文件,而这些代码似乎并不存在。相反,下面的代码在"文件"中创建文本文件。与"缓存"在同一目录中的文件夹文件夹中。

FileOutputStream fOut = null;
            try {
                fOut = openFileOutput("secret.txt",MODE_PRIVATE);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            String str = "data";
            try {
                fOut.write(str.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

我的问题是,如何正确指定" MySecretFolder"将文本文件设为?

我尝试了以下内容:

" /data/data/com.example.myandroid.cuecards/cache/MySecretFolder" ,但是如果我尝试的话,它会崩溃我的整个应用程序。我该如何在缓存/ MySecretFolder 中正确保存文本文件?

4 个答案:

答案 0 :(得分:3)

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

File myDir = new File(getCacheDir(), "folder");
myDir.mkdir();

请尝试这可能会对您有所帮助。

确定如果要在“指定文件夹”中创建TextFile,则可以尝试使用代码。

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();
    }

答案 1 :(得分:1)

只需更改

LogInfo

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

这将使 MySecretFolder

下的 secret.txt

答案 2 :(得分:1)

创建文件夹,文件并写入/添加到文件中的简单代码

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>

答案 3 :(得分:0)

getPrivateDir将在您的私有区域中创建一个文件夹(Context.MODE_WORLD_WRITEABLE-使用适合您的Context.MODE _...)

public File getPrivateDir(String name)
{
    return context.getDir(name, Context.MODE_WORLD_WRITEABLE);
}

如果文件目录中的私人文件夹中不存在,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