1. import java.io.*;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.view.View.OnClickListener;
  6. import android.widget.*;
  7. public class FileIOTest extends Activity {
  8. private LinearLayout mainView=null;
  9. private Button writeButton=null;
  10. private Button readButton=null;
  11. private TextView tv=null;
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. writeButton=new Button(this);
  15. writeButton.setText("文件写入");
  16. writeButton.setOnClickListener(new OnClickListener(){
  17. public void onClick(View v) {
  18. fileWrite();
  19. }
  20. });
  21. readButton=new Button(this);
  22. readButton.setEnabled(false);
  23. readButton.setText("文件读出");
  24. readButton.setOnClickListener(new OnClickListener(){
  25. public void onClick(View v) {
  26. fileRead();
  27. }
  28. });
  29. tv=new TextView(this);
  30. tv.setText("这里显示读出结果");
  31. mainView=new LinearLayout(this);
  32. mainView.setOrientation(LinearLayout.VERTICAL);
  33. mainView.addView(writeButton);
  34. mainView.addView(readButton);
  35. mainView.addView(tv);
  36. setContentView(mainView);
  37. }
  38. /*文件写*/
  39. void fileWrite(){
  40. //File file=this.getFilesDir();//打开私有目录
  41. File file=new File("/sdcard");
  42. String path=file.getAbsolutePath();//获取路径
  43. String name="mydata1.dat";//新建文件名
  44. File filex=new File(path,name);
  45. //如果文件不存在,则创建一个文件
  46. if(!filex.exists()){
  47. try {
  48. filex.createNewFile();
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. //获取文件输出流
  54. FileOutputStream fos=null;
  55. try {
  56. fos=new FileOutputStream(filex);
  57. byte buf[]="Hello,这是Android入门之文件操作(三)文件读写!".getBytes();
  58. //上面涉及到字符串转字符,为了保证编码正常,建议采用下面的方法
  59. //ByteArrayOutputStream baos=new ByteArrayOutputStream();
  60. //DataOutputStream dos=new DataOutputStream(baos);
  61. //try {dos.writeUTF("XXXXXXXXXXXXXXXXX");catch (IOException e1) {e1.printStackTrace();}
  62. //byte[] buf=baos.toByteArray();
  63. try {
  64. fos.write(buf);//全面覆盖式的写,如果要添加或者修改,得把原来的先读出来再做处理
  65. fos.close();
  66. } catch (IOException e) {
  67. e.printStackTrace();
  68. }
  69. } catch (FileNotFoundException e) {
  70. e.printStackTrace();
  71. }
  72. readButton.setEnabled(true);
  73. }
  74. /*文写读*/
  75. void fileRead(){
  76. //File file=this.getFilesDir();//打开私有目录
  77. File file=new File("/sdcard");
  78. String path=file.getAbsolutePath();//获取路径
  79. String name="mydata1.dat";//新建文件名
  80. File filex=new File(path,name);
  81. //
  82. try {
  83. FileInputStream fis=new FileInputStream(filex);
  84. byte buf[]=new byte[1024];
  85. try {
  86. int len=fis.read(buf);
  87. fis.close();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. //显示读取结果
  92. tv.setText(new String(buf));
  93. //上面涉及字符转字符串,为了保证编码正常,建议采用下面的方法
  94. //ByteArrayInputStream bais=new ByteArrayInputStream(buf);
  95. //DataInputStream dis=new DataInputStream(bais);
  96. //try {tv.setText(dis.readUTF());} catch (IOException e) {e.printStackTrace();}
  97. } catch (FileNotFoundException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101. }

Androlid入门之文件系统操作(三)文件读写的更多相关文章

  1. Android入门之文件系统操作(一)简单的文件浏览器 (转)

    Android入门之文件系统操作(一)简单的文件浏览器 (转)        import java.io.File; import java.util.*; import android.app.A ...

  2. Android入门之文件系统操作

    Android入门之文件系统操作(二)文件操作相关指令 (转)   (一)获取总根 File[] fileList=File.listRoots(); //返回fileList.length为1 // ...

  3. QSettings配置读写-win注册表操作-ini文件读写

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QSettings配置读写-win注册表操作-ini文件读写     本文地址:http:// ...

  4. ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调

    近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...

  5. [转]ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调

    本文转自:http://www.cnblogs.com/artwl/p/3396330.html 近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0, ...

  6. python基础操作_文件读写操作

    #文件读写# r只能读不能写,且文件必须存在,w只能写不能读,a只能写不能读# w+是写读模式,清空原文件内容# r+是读写模式,没有清空原文件内容,# 只要有r,文件必须存在,只要有w,都会清空原文 ...

  7. Android入门之文件系统操作(二)文件操作相关指令

    (一)获取总根 File[] fileList=File.listRoots(); //返回fileList.length为1 //fileList.getAbsolutePath()为"/ ...

  8. Android入门之文件系统操作(一)简单的文件浏览器

    版权声明:本文为博主原创文章,未经博主允许不得转载.       import java.io.File; import java.util.*; import android.app.Activit ...

  9. .net对文件的操作之文件读写

    读写文件的步骤一般需要5步: 创建文件流 创建读写器 执行读或写的操作 关闭读写器 关闭文件流 需要引用:System.IO这个命名空间 代码演示: string path = @"F:\a ...

随机推荐

  1. (转)容易遗忘的JS知识点整理

    1.hasOwnProperty相关 为了判断一个对象是否包含自定义属性而不是原型链上的属性,我们需要使用继承自 Object.prototype 的 hasOwnProperty方法.hasOwnP ...

  2. Json——Json与JS的区别

    JSON是JS的字面量的特殊表现形式,它使用文本表示 JS 对象信息,本质是字符串 var obj = {a: 'Hello', b: 'World'}; //这是一个对象,注意键名也是可以使用引号包 ...

  3. 开源业务规则引擎JBoss Drools

    Drools 是什么? 规则引擎由推理引擎发展而来,是一种嵌入在应用程序中的组件,实现了将业务决策从应用程序代码中分离出来,并使用预定义的语义模块编写业务决策.接受数据输入,解释业务规则,并根据业务规 ...

  4. Caffe RPN :error C2220: warning treated as error - no 'object' file generated

    在 caffe里面添加rpn_layer.cpp之后,总是出现 error C2220: warning treated as error - no 'object' file generated 这 ...

  5. cookie范例

    GET /locate/api/getLocByIp?key=C6E22B7D480E3312C74EC7EF013E50C5&callback=bowlder.cb._0 HTTP/1.1 ...

  6. 通过offset值的设置使html元素对齐

    今天是我第一次写这个随笔,为了记录我发现的一个jquery的offset的值的问题. 这个offset的值会因为页面标签是否处于隐藏状态而表现出不同的值,隐藏状态时,offset的值是相对于直接父亲的 ...

  7. 用 ilasm 反编译、修改.net dll文件

    有些.net dll我们没有源码,如果要修改某些东西,可以用ilasm.exe反编译为il代码,修改后再编译回dll ilasm通常放在以下路径 C:\Windows\Microsoft.NET\Fr ...

  8. CAD绘一个文字自动剧中的标注 (com接口)

    主要用到函数说明: _DMxDrawX::DrawDimRotated 绘制一个线型标注.详细说明如下: 参数 说明 DOUBLE dExtLine1PointX 输入第一条界线的起始点X值 DOUB ...

  9. CAD读取属性块

    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 3 ...

  10. thinkphp里模版文件js无法使用if condition的问题

    /**     * @example   thinkphp里模版文件js无法使用if condition的问题     * @example  参考地址:https://segmentfault.co ...