将数据写入Internal Storage:

 String fileName = "myfile.txt";
String str="保存数据到内部存储";
try {
FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(str.getBytes());
fos.flush();
fos.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

从Internal Storage读取数据:

 try {
FileInputStream fis = openFileInput("myfile.txt");
byte[] data = new byte[fis.available()];
fis.read(data);
fis.close(); textView.setText(new String(data)); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

将数据写入External Storage:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

                     File file = new File(Environment.getExternalStorageDirectory(), "myfile.txt");

                     if(!file.exists())
{
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(editText.getText().toString().getBytes());
fos.close(); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

从External Storage读取数据:

 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

                     File file = new File(Environment.getExternalStorageDirectory(), "myfile.txt");

                     try {
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[fis.available()]; fis.read(data);
fis.close(); textView.setText(new String(data)); } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }

在Cache目录创建文件:

 try {
File file = File.createTempFile("mycache", "txt", getCacheDir()); FileOutputStream fos = new FileOutputStream(file);
fos.write(editText.getText().toString().getBytes());
fos.close(); } catch (IOException e) {
e.printStackTrace();
}

Android文件操作的更多相关文章

  1. android 文件操作类简易总结

    android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...

  2. Java和Android文件操作

    File这是文件基类,抽象地代表一个文件实体,它有四个不同的构造方法: File(File dir, String name)  File(String path)   File(String dir ...

  3. Android 文件操作心得体会

    android 的文件操作说白了就是Java的文件操作的处理.所以如果对Java的io文件操作比较熟悉的话,android的文件操作就是小菜一碟了.好了,话不多说,开始今天的正题吧. 先从一个小项目入 ...

  4. Android 文件操作之openFileOutput

    openFileOutput()方法的第一参数用于指定文件名称,不能包含路径分隔符“/” ,如果文件不存在,Android 会自动创建它.创建的文件保存在/data/data/<package ...

  5. 大过年的,不下班的,上个Android文件操作类(内部存储和sd卡均可)

    package com.kkdiangame.UI.res; import java.io.ByteArrayOutputStream; import java.io.File; import jav ...

  6. 转:Android文件操作总结

    http://www.cnblogs.com/devinzhang/archive/2012/01/19/2327597.html http://blog.sina.com.cn/s/blog_5a4 ...

  7. Android文件操作报open failed: EBUSY (Device or resource busy)

    Android删除文件后重新创建时偶尔出现 open failed: EBUSY (Device or resource busy)错误,该错误是Android系统的一个bug,大概的意思类似于win ...

  8. 转 Android - 文件操作

    一.资源文件的读取: 1) 从resource的raw中读取文件数据: String res = ""; try{ //得到资源中的Raw数据流 InputStream in = ...

  9. Android数据存储-文件操作

    一.预备知识 1.Android中的MVC设计模式 MVC (Model-View-Controller):M是指逻辑模型,V是指视图模型,C则是控制器.一个逻辑模型可以对于多种视图模型,比如一批统计 ...

随机推荐

  1. Ip 地址

    访问 ip.mayfirst.org可以显示你的ip地址,如果你可以联网的话.

  2. Android View绘制过程

    Android的View绘制是从根节点(Activity是DecorView)开始,他是一个自上而下的过程.View的绘制经历三个过程:Measure.Layout.Draw.基本流程如下图: per ...

  3. Second Level Cache for Entity Framework 6.1

    Second Level Cache for Entity Framework 6.1 Entity Framework does not currently support caching of q ...

  4. 个人对joomla3.2x和joomla2.5X浅薄看法

    很久没有写joomla文章了,发现想写的东西还是挺多的,后面抽时间补回来,其实更多还是php的一些东西.joomla3.0以后系统改变挺大,后台都是用bootstrap作为主题,个人对这个无爱,因为他 ...

  5. shell 和awk性能对比

    time for ((i=0;i<10000;i++)) do ((sum+=i)); done real    0m0.086suser    0m0.079ssys    0m0.007s ...

  6. 【Android菜鸟学习之路】环境搭建问题-修改AVD Path

    更改avd默认路径

  7. gulp-imagemin图片压缩----gulp系列(三)

    本节实现图片压缩,在实现压缩前,先配置images任务,设置源目录和输出目录. 在系列(二)代码的基础上,再进行扩展. 1.找到gulp->config.js,对images进行源目录(src- ...

  8. SQL Server 2012:SQL Server体系结构——一个查询的生命周期(第2部分)

    计划缓存(Plan Cache) 如果SQL Server已经找到一个好的方式去执行一段代码时,应该把它作为随后的请求重用,因为生成执行计划是耗费时间且资源密集的,这样做是有有意义的. 如果没找到被缓 ...

  9. js-弹出窗口

    一.JS最常用三种弹出对话框 1.对话框 //弹出对话框并输出一段提示信息 function ale() { //弹出一个对话框 alert("提示信息!"); } 2.询问框 / ...

  10. MyBatis魔法堂:各数据库的批量Update操作

    一.前言   MyBatis的update元素的用法与insert元素基本相同,因此本篇不打算重复了.本篇仅记录批量update操作的sql语句,懂得SQL语句,那么MyBatis部分的操作就简单了. ...