Android数据存储之文件 openFileOutput & openFileInput
来源:互联网 发布:中科院数据共享平台 编辑:程序博客网 时间:2024/05/20 07:31
在Context类中提供了抽象方法:
/** * Open a private file associated with this Context's application package * for reading. * * @param name The name of the file to open; can not contain path * separators. * * @return The resulting {@link FileInputStream}. * * @see #openFileOutput * @see #fileList * @see #deleteFile * @see java.io.FileInputStream#FileInputStream(String) */ public abstract FileInputStream openFileInput(String name) throws FileNotFoundException;
/** * Open a private file associated with this Context's application package * for writing. Creates the file if it doesn't already exist. * * @param name The name of the file to open; can not contain path * separators. * @param mode Operating mode. Use 0 or {@link #MODE_PRIVATE} for the * default operation, {@link #MODE_APPEND} to append to an existing file, * {@link #MODE_WORLD_READABLE} and {@link #MODE_WORLD_WRITEABLE} to control * permissions. * * @return The resulting {@link FileOutputStream}. * * @see #MODE_APPEND * @see #MODE_PRIVATE * @see #MODE_WORLD_READABLE * @see #MODE_WORLD_WRITEABLE * @see #openFileInput * @see #fileList * @see #deleteFile * @see java.io.FileOutputStream#FileOutputStream(String) */ public abstract FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException;所以在Activity、Service等类中直接使用这两个方法就可以了。
1.openFileOutput方法将文件保存在/data/data/<package name>/files目录下。
OutputStream os = openFileOutput("zhangmq.txt", Activity.MODE_PRIVATE);
2.在使用openFileInput方法获得InputStream对象来读取文件中的数据的时候,只需要指定文件名即可。
InputStream is = openFileInput("zhangmq.txt");
3.实例
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); text = (TextView)findViewById(R.id.text); try { OutputStream os = openFileOutput("zhangmq.txt", Activity.MODE_PRIVATE); String str = "zhangmq"; try { os.write(str.getBytes("utf-8"));// os.write(str.getBytes()); os.close(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } InputStream is = openFileInput("zhangmq.txt"); byte[] buffer = new byte[1024]; try { int byteCount = is.read(buffer);// String bufferStr = buffer.toString(); String bufferStr = new String(buffer,0,byteCount,"utf-8"); text.setText(bufferStr); is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
也算是比较简单的存储了。
0 0
- Android数据存储之文件 openFileOutput & openFileInput
- 文件存储openFileOutput和openFileInput
- 文件存储openFileOutput和openFileInput
- 文件存储openFileOutput和openFileInput
- 使用openFileInput和openFileOutput实现Android平台的数据存储
- Android之openFileoutput和openFileinput
- android 文件存储(openFileOutPut 和 openFileInput())
- 内部存储之 openfileinput openfileoutput使用
- Android文件操作中的openFileOutPut和openFileInput
- Android openFileOutput 和 openFileInput
- Android openFileOutput 和 openFileInput
- openFileInput()和 openFileOutput()方法利用java流将数据存储到文件中
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- Android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- 文件存储openFileOutput和openFileInput 和String.getBytes()
- 读写内部存储的文件数openFileOutput(),openFileInput()
- 本地存储(openFileInput,openFileOutput)
- HDU 1238 Substrings(求公共正反向连续子串)
- 算法代码:查找数组中第i小的元素
- 关于Java中多个数据excel表格的导出
- Linux线程通信条件变量理解
- VS2010/MFC编程之树型控件的创建及CTreeCtrl类的成员函数和实例
- Android数据存储之文件 openFileOutput & openFileInput
- 测试用例知识,总结边界值,因果图,等价类划分法
- win7跨网段共享解决方法
- HDU - 2844 Coins(多重背包)
- HDU 4912 Paths on the tree(LCA+贪心)
- OpenCV 学习(几种基本的低通滤波)
- CF479 Riding in a Lift
- SaltStack学习笔记
- HDU - 3732 Ahui Writes Word(多重背包)