Android中的文件存储数据方式
2011-07-19 17:11
519 查看
1.文件存储数据使用了Java中的IO操作来进行文件的保存和读取,只不过Android在Context类中封装好了输入流和输出流的获取方法。
创建的存储文件保存在/data/data/<package name>/files文件夹下。
2.操作。
保存文件内容:通过Context.openFileOutput获取输出流,参数分别为文件名和存储模式。
读取文件内容:通过Context.openFileInput获取输入流,参数为文件名。
删除文件:Context.deleteFile删除指定的文件,参数为将要删除的文件的名称。
获取文件名列表:通过Context.fileList获取files目录下的所有文件名数组。
*获取文件路径的方法:
绝对路径:/data/data/<package name>/files/filename
Context:Context.getFilesDir()可以获取到”/data/data/<package name>/files”
3.四种文件保存的模式。
Context.MODE_PRIVATE 为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下写入的内容会覆盖原文件的内容。
Context.MODE_APPEND 检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
MODE_WORLD_READABLE 表示当前文件可以被其他应用读取。
MODE_WORLD_WRITEABLE 表示当前文件可以被其他应用写入。
在使用模式时,可以用”+”来选择多种模式,比如openFileOutput(FILENAME, Context.MODE_PRIVATE + MODE_WORLD_READABLE);
下面通过程序来演示下文件存储的使用。完整代码下载:
点击链接下载android_files
大小 : 44.29 kB
下载次数 : 0
程序截图:
提供一个文件存储数据的工具类:
创建的存储文件保存在/data/data/<package name>/files文件夹下。
2.操作。
保存文件内容:通过Context.openFileOutput获取输出流,参数分别为文件名和存储模式。
读取文件内容:通过Context.openFileInput获取输入流,参数为文件名。
删除文件:Context.deleteFile删除指定的文件,参数为将要删除的文件的名称。
获取文件名列表:通过Context.fileList获取files目录下的所有文件名数组。
*获取文件路径的方法:
绝对路径:/data/data/<package name>/files/filename
Context:Context.getFilesDir()可以获取到”/data/data/<package name>/files”
3.四种文件保存的模式。
Context.MODE_PRIVATE 为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下写入的内容会覆盖原文件的内容。
Context.MODE_APPEND 检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
MODE_WORLD_READABLE 表示当前文件可以被其他应用读取。
MODE_WORLD_WRITEABLE 表示当前文件可以被其他应用写入。
在使用模式时,可以用”+”来选择多种模式,比如openFileOutput(FILENAME, Context.MODE_PRIVATE + MODE_WORLD_READABLE);
下面通过程序来演示下文件存储的使用。完整代码下载:
点击链接下载android_files
大小 : 44.29 kB
下载次数 : 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | /** * MainActivity * * @author zuolongsnail * */ public class MainActivity extends Activity { private EditText writeET; private Button writeBtn; private TextView contentView; public static final String FILENAME = "setting.set"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); writeET = (EditText) findViewById(R.id.write_et); writeBtn = (Button) findViewById(R.id.write_btn); contentView = (TextView) findViewById(R.id.contentview); writeBtn.setOnClickListener(new OperateOnClickListener()); } class OperateOnClickListener implements OnClickListener { @Override public void onClick(View v) { writeFiles(writeET.getText().toString()); contentView.setText(readFiles()); System.out.println(getFilesDir()); } } // 保存文件内容 private void writeFiles(String content) { try { // 打开文件获取输出流,文件不存在则自动创建 FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(content.getBytes()); fos.close(); } catch (Exception e) { e.printStackTrace(); } } // 读取文件内容 private String readFiles() { String content = null; try { FileInputStream fis = openFileInput(FILENAME); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) != -1) { baos.write(buffer, 0, len); } content = baos.toString(); fis.close(); baos.close(); } catch (Exception e) { e.printStackTrace(); } return content; } } |
提供一个文件存储数据的工具类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | /** * 文件存储数据方式工具类 * * @author zuolongsnail */ public class FilesUtil { /** * 保存文件内容 * * @param c * @param fileName * 文件名称 * @param content * 内容 */ private void writeFiles(Context c, String fileName, String content, int mode) throws Exception { // 打开文件获取输出流,文件不存在则自动创建 FileOutputStream fos = c.openFileOutput(fileName, mode); fos.write(content.getBytes()); fos.close(); } /** * 读取文件内容 * * @param c * @param fileName * 文件名称 * @return 返回文件内容 */ private String readFiles(Context c, String fileName) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileInputStream fis = c.openFileInput(fileName); byte[] buffer = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) != -1) { baos.write(buffer, 0, len); } String content = baos.toString(); fis.close(); baos.close(); return content; } } |
相关文章推荐
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android数据保存的3种方式 SharedPreferences存储、文件存储、数据库存储SQLite 未完
- 实例详解Android文件存储数据方式
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android中的文件存储数据方式
- android学习记录3(数据存储的方式:sqlite、sp、存文件。listview简单使用)
- Android数据存储方式2.文件存储数据
- Android数据存储方式(一)文件读写操作
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android中关于数据存储的方式--文件存储
- android数据存储之文件方式和SharedPreference
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- Android中的文件存储数据方式
- Android中数据存储方式一:文件形式
- Android的数据存储方式------文件存储数据
- Android的5种数据存储方式之文件存储
- Android学习 —— 数据的存储与访问方式一: 文件存取
- android 数据存储----文件方式 总结
- android 数据存储和访问方式一:文件详解