public abstract File getCacheDir ()
Returns the absolute path to the application specific cache directory on the filesystem. These files will be ones that get deleted first when the device runs low on storage. There is no guarantee when these files will be deleted. Note: you should not rely on the system deleting these files for you; you should always have a reasonable maximum, such as 1 MB, for the amount of space you consume with cache files, and prune those files when exceeding that space.
该目录主要用于存放缓存文件,当系统的内存存储空间紧张时,该目录下的文件会被删除掉。关于这些文件究竟会在存储空间剩余多少的情况,没有严格的标准保障。
注意:你不应该依赖系统来清理这些缓存文件,你应该对这些缓存文件占用的最大存储空间设定个最大值,比如是1M,当实际占用空间超过这个值时,你应该对这些缓存文件做相应的清理工作(prune)。
Returns
- Returns the path of the directory holding application cache files.
See Also
openFileOutput(String, int)
getFileStreamPath(String)
getDir(String, int)
- 示例1
- import android.app.Activity;
- import android.content.Context;
- import android.os.Bundle;
- import android.util.Log;
- public class MainActivity extends Activity {
- final static String TAG="robin";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Context context=this;
- String path=context.getCacheDir().getAbsolutePath();
- Log.i(TAG,"path:"+path);
- }
- }
- 运行结果
- 10-01 14:57:52.296: I/robin(7835): path:/data/data/com.lenovo/cache
public abstract File getDir (String name, int mode)
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.
该函数主要用于得到一个文件夹的句柄,并通过该句柄创建和访问外文件夹。
注意:参数int mode是指文件夹的访问权限而并不包括其子文件夹和文件的访问权限
Parameters
name | Name of the directory to retrieve. This is a directory that is created as part of your application data. |
---|---|
mode | Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions. |
Returns
- Returns a File object for the requested directory. The directory will have been created if it does not already exist.
See Also
openFileOutput(String, int)
- 示例2
- File file=context.getDir("download", Context.MODE_PRIVATE);
- String path=file.getAbsolutePath();
- Log.i(TAG,"path:"+path);
- 运行结果
- 10-02 08:56:49.278: I/robin(12055): path:/data/data/com.lenovo/app_download
public abstract File getFileStreamPath (String name)
Returns the absolute path on the filesystem where a file created with openFileOutput(String, int)
is stored.
Parameters
name | The name of the file for which you would like to get its path. |
---|
Returns
- Returns an absolute path to the given file.
See Also
openFileOutput(String, int)
getFilesDir()
getDir(String, int)
- 示例3
- File file=context.getFileStreamPath("download");
- String path=file.getAbsolutePath();
- Log.i(TAG,"path:"+path);
- 运行结果
- 10-02 09:17:55.913: I/robin(12507): path:/data/data/com.lenovo/files/download
public abstract File getFilesDir ()
Returns the absolute path to the directory on the filesystem where files created with openFileOutput(String, int)
are stored.
Returns
- Returns the path of the directory holding application files.
See Also
openFileOutput(String, int)
getFileStreamPath(String)
getDir(String, int)
File file=context.getFilesDir();String path=file.getAbsolutePath();Log.i(TAG,"path:"+path);
public abstract FileInputStream openFileInput (String name)
Open a private file associated with this Context's application package for reading.
Parameters
name | The name of the file to open; can not contain path separators. |
---|
Returns
- FileInputStream Resulting input stream.
Throws
FileNotFoundException |
---|
See Also
openFileOutput(String, int)
fileList()
deleteFile(String)
FileInputStream(String)
public abstract FileOutputStream openFileOutput (String name, int mode)
Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.
Parameters
name | The name of the file to open; can not contain path separators. |
---|---|
mode | Operating mode. Use 0 or MODE_PRIVATE for the default operation, MODE_APPEND to append to an existing file, MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE to control permissions. |
Returns
- FileOutputStream Resulting output stream.
Throws
FileNotFoundException |
---|
See Also
MODE_APPEND 表示写文件时是追加模式,即从文件末开始写数据
MODE_PRIVATE
MODE_WORLD_READABLE
MODE_WORLD_WRITEABLE
openFileInput(String)
fileList()
deleteFile(String)
FileOutputStream(String)
String FILENAME = "hello_file";String string = "hello world!";FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);fos.write(string.getBytes());fos.close();
String FILENAME = "hello_file";String string = "hello world!";FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);fos.write(string.getBytes());fos.close();
String FILENAME = "hello_file";String string = "hello world!";FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND|Context.MODE_WORLD_READABLE;fos.write(string.getBytes());fos.close();
public abstract boolean deleteFile (String name)
Delete the given private file associated with this Context's application package.
Parameters
name | The name of the file to delete; can not contain path separators. |
---|
Returns
- True if the file was successfully deleted; else false.
See Also
openFileInput(String)
openFileOutput(String, int)
fileList()
delete()
public abstract String[] fileList ()
Returns an array of strings naming the private files associated with this Context's application package.
Returns
- Array of strings naming the private files.
See Also
openFileInput(String)
openFileOutput(String, int)
deleteFile(String)
public static File getDataDirectory ()
Gets the Android data directory.
用File返回数据文件的根目录,返回的文件的路径为“/data”。该目录下的文件是只读。应用程序无法对该目录下的文件进行写操作。
public static File getDownloadCacheDirectory ()
Gets the Android Download/Cache content directory.
用File返回缓存文件的根目录,返回的文件的路径为“/cache”。对于第三方应用程序。该目录下的文件是只读。第三方应用程序无法对该目录下的文件进行写操作。
public static File getRootDirectory ()
Gets the Android root directory.
用File返回Android系统文件的根目录,返回的文件的路径为“/system”。该目录下的文件是只读。应用程序无法对该目录下的文件进行写操作。