name>/files),其它程序无法訪问。除非在创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE ,仅仅有这样其它程序才能正确訪问。
name>/cache文件夹
name>/files文件夹
public class FileService
{
private Context context;
public FileService(Context
context)
{
this.context =
context;
}
/**
* openFileOutput()方法的第一參数用于指定文件名,不能包括路径分隔符“/” ,假设文件不存在,Android
* 会自己主动创建它。
创建的文件保存在/data/data/<package
name>/files文件夹。如:
* /data/data/cn.itcast.action/files/itcast.txt ,通过点击Eclipse菜单“Window”-“Show
* View”-“Other”,在对话窗体中展开android目录。选择以下的File
Explorer视图。然后在File
* Explorer视图中展开/data/data/<package
name>/files文件夹就能够看到该文件。
*/
public void save(String
fileName, String content) throws Exception
{
/*
openFileOutput()方法的第二參数用于指定操作模式,有四种模式。分别为:
* Context.MODE_PRIVATE = 0
* Context.MODE_APPEND = 32768
* Context.MODE_WORLD_READABLE = 1
* Context.MODE_WORLD_WRITEABLE = 2
*/
FileOutputStream stream = context.openFileOutput(fileName,
Context.MODE_PRIVATE);
stream.write(content.getBytes());
stream.close();
}
/**
* 读取文件内容
* @param fileName 文件名
* @return
* @throws Exception
*/
public String
readFile(String fileName) throws Exception
{
String fileContent = "";
StringBuffer content = new StringBuffer();
FileInputStream stream = context.openFileInput(fileName);
byte[]
buffer = new byte[1024];
int len
= 0;
while((len=stream.read(buffer))
!= -1)
{
fileContent = new String(buffer,
0, len);
content.append(fileContent);
}
System.out.println(content.toString());
return content.toString();
}
}