将用户输入的文本保存到android文件系统中。文件被保存在android文件系统的/data/data/your.packet.name.appname/files/folder, 查看这个文件可以通过在eclipse -> Windows -> Open Perspective > Other -> DDMS-> File Explore -> data -> data ...-> samplefile.txt 选中后 点击右上脚 “Pull a file from device”

1. SaveFile.java

public class ReadFile extends Activity implements View.OnClickListener { private static final String TAG = "ReadFileActivity"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Button readButton = (Button) findViewById(R.id.button1); readButton.setOnClickListener(this); Button clearButton = (Button) findViewById(R.id.clearbutton); clearButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { TextView textArea = (TextView) findViewById(R.id.filecontent); textArea.setText(""); } }); } public void onClick(View arg0) { doReadFromFile(this); } private void doReadFromFile(Context context) { InputStream istream = null; BufferedReader br = null; String fileContent = "", line=""; try { istream = (InputStream) context.getResources().openRawResource(R.raw.mytext); br = new BufferedReader(new InputStreamReader(istream)); while ( (line = br.readLine()) != null) { fileContent += line + "/n"; } br.close(); istream.close(); } catch (Exception e){ Log.e(TAG, "Found exception when reading file: " + e); } Log.i(TAG, "file content = " + fileContent ); TextView showContent = (TextView) findViewById(R.id.filecontent); showContent.setText(fileContent); } }

更多相关文章

  1. 【Android开发学习06】Android中的文件I/O操作
  2. 关于xml文件中的一些常用标签的意义
  3. Android中资源文件夹res/raw和assets的使用
  4. [Android Studio / NDK] 如何使用javah生成.h文件
  5. Android 打开本地文件兼容7.0
  6. [Google Android] 理解NDK(1)-- 编译Android .so文件
  7. Android开发8——利用pull解析器读写XML文件
  8. Android 自动编译、打包生成apk文件 、命令行方式
  9. Android编程之文件操作

随机推荐

  1. 敖丙读者字节、滴滴面经(已拿offer)
  2. 0322作业(选择器的优先级,id,class,tag);
  3. 0322作业-CSS基础1
  4. nginx代理
  5. 实力认证|Authing 入选 2021 《中国网络安
  6. redis与lua
  7. nginx 负载均衡
  8. 进程终止,环境表以及在内存中布局和非局
  9. linux学习基本
  10. Spark 3.0 中七个必须知道的 SQL 性能优