Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1294813
  • 博文数量: 175
  • 博客积分: 2743
  • 博客等级: 少校
  • 技术积分: 4024
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-30 01:41
文章分类

全部博文(175)

文章存档

2015年(1)

2013年(53)

2012年(71)

2011年(50)

分类: Java

2011-11-25 17:21:56

    

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

阅读(1567) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~