zoukankan      html  css  js  c++  java
  • android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()

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

    main.xml

    [xhtml] view plaincopy
     
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="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 plaincopy
     
    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为我们提供了两个方法来获取输入输出流。简单、方便、快捷啊。

    http://blog.csdn.net/chenzheng_java/article/details/6214261

  • 相关阅读:
    CentOS6.3升级GCC到GCC4.8.2
    监督式学习 -- 分类决策树(一)
    DICOM医学图像处理:fo-dicom网络传输之 C-Echo and C-Store
    百度地图-----&gt;地图类型、定位模式、实时交通、我的位置、加入覆盖物、覆盖物详情及提示
    &quot;浪潮杯&quot;第六届ACM山东省省赛山科场总结
    标题栏风格设置
    ActionBarActivity设置全屏无标题
    王立平--自己定义TitleBar
    C++ const限定符
    黑马day14 过滤器概述&amp;生命周期&amp;运行过程
  • 原文地址:https://www.cnblogs.com/daishuguang/p/3868601.html
Copyright © 2011-2022 走看看