【android】:android文件读写

最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示。

main.xml

[xhtml]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:<a href="http://lib.csdn.net/base/15" class='replace_word' title="undefined" target='_blank' style='color:#df3434; font-weight:bold;'>Android</a>="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="请您输入要保存的内容:"  
  11.     />  
  12.  <EditText  
  13.     android:id="@+id/addText"  
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content"  
  16.     android:hint="请您在此处输入文件内容!"  
  17.  />     
  18.  <Button   
  19.     android:id="@+id/addButton"  
  20.     android:layout_width="wrap_content"   
  21.     android:layout_height="wrap_content"  
  22.     android:text="save"  
  23.  />  
  24.  <Button  
  25.     android:id="@+id/showButton"  
  26.     android:layout_width="wrap_content"   
  27.     android:layout_height="wrap_content"  
  28.     android:text="show"  
  29.  />  
  30.  <TextView  
  31.     android:id="@+id/showText"    
  32.     android:layout_width="fill_parent"   
  33.     android:layout_height="wrap_content"   
  34.     />  
  35.    
  36. </LinearLayout>  

 

 

activity代码

 

[java]  view plain  copy
  1. package cn.com.file;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8.   
  9. import android.app.Activity;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13. import android.widget.Button;  
  14. import android.widget.EditText;  
  15. import android.widget.TextView;  
  16. import android.widget.Toast;  
  17.   
  18. public class FileTest extends Activity {  
  19.     private EditText editText;  
  20.     private TextView showTextView;  
  21.     // 要保存的文件名  
  22.     private String fileName = "chenzheng_java.txt";  
  23.   
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.main);  
  28.         // 获取页面中的组件  
  29.         editText = (EditText) findViewById(R.id.addText);  
  30.         showTextView = (TextView) findViewById(R.id.showText);  
  31.         Button addButton = (Button) this.findViewById(R.id.addButton);  
  32.         Button showButton = (Button) this.findViewById(R.id.showButton);  
  33.         // 绑定单击事件  
  34.         addButton.setOnClickListener(listener);  
  35.         showButton.setOnClickListener(listener);  
  36.   
  37.     }  
  38.   
  39.     // 声明监听器  
  40.     private View.OnClickListener listener = new OnClickListener() {  
  41.         public void onClick(View v) {  
  42.             Button view = (Button) v;  
  43.             switch (view.getId()) {  
  44.             case R.id.addButton:  
  45.                 save();  
  46.                 break;  
  47.             case R.id.showButton:  
  48.                 read();  
  49.                 break;  
  50.   
  51.             }  
  52.   
  53.         }  
  54.   
  55.     };  
  56.   
  57.     /** 
  58.      *@author chenzheng_Java  
  59.      *保存用户输入的内容到文件 
  60.      */  
  61.     private void save() {  
  62.   
  63.         String content = editText.getText().toString();  
  64.         try {  
  65.             /* 根据用户提供的文件名,以及文件的应用模式,打开一个输出流.文件不存系统会为你创建一个的, 
  66.              * 至于为什么这个地方还有FileNotFoundException抛出,我也比较纳闷。在Context中是这样定义的 
  67.              *   public abstract FileOutputStream openFileOutput(String name, int mode) 
  68.              *   throws FileNotFoundException; 
  69.              * openFileOutput(String name, int mode); 
  70.              * 第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名 
  71.              *          该文件会被保存在/data/data/应用名称/files/chenzheng_java.txt 
  72.              * 第二个参数,代表文件的操作模式 
  73.              *          MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖 
  74.              *          MODE_APPEND  私有   重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件 
  75.              *          MODE_WORLD_READABLE 公用  可读 
  76.              *          MODE_WORLD_WRITEABLE 公用 可读写 
  77.              *  */  
  78.             FileOutputStream outputStream = openFileOutput(fileName,  
  79.                     Activity.MODE_PRIVATE);  
  80.             outputStream.write(content.getBytes());  
  81.             outputStream.flush();  
  82.             outputStream.close();  
  83.             Toast.makeText(FileTest.this"保存成功", Toast.LENGTH_LONG).show();  
  84.         } catch (FileNotFoundException e) {  
  85.             e.printStackTrace();  
  86.         } catch (IOException e) {  
  87.             e.printStackTrace();  
  88.         }  
  89.   
  90.     }  
  91.   
  92.     /** 
  93.      * @author chenzheng_java  
  94.      * 读取刚才用户保存的内容 
  95.      */  
  96.     private void read() {  
  97.         try {  
  98.             FileInputStream inputStream = this.openFileInput(fileName);  
  99.             byte[] bytes = new byte[1024];  
  100.             ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();  
  101.             while (inputStream.read(bytes) != -1) {  
  102.                 arrayOutputStream.write(bytes, 0, bytes.length);  
  103.             }  
  104.             inputStream.close();  
  105.             arrayOutputStream.close();  
  106.             String content = new String(arrayOutputStream.toByteArray());  
  107.             showTextView.setText(content);  
  108.   
  109.         } catch (FileNotFoundException e) {  
  110.             e.printStackTrace();  
  111.         } catch (IOException e) {  
  112.             e.printStackTrace();  
  113.         }  
  114.   
  115.     }  
  116.   
  117. }  

其他的都为默认。

关于文件保存的路径可以通过ADT携带的File Explorer工具进行查看。如何调出File Explorer工具呢;我们可以通过Windows--showView--others-android下面看到File Explorer。这里是我的一个截图。

 

对于这个程序,基本上没什么难点,就是纯粹的java流知识。唯一不同的就是context为我们提供了两个方法来获取输入输出流。简单、方便、快捷啊。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值