当前位置: 移动技术网 > IT编程>移动开发>Android > Android应用程序中读写txt文本文件的基本方法讲解

Android应用程序中读写txt文本文件的基本方法讲解

2019年07月24日  | 移动技术网IT编程  | 我要评论

玉林高中,随州市教育考试中心,山狗2003兽性陷阱

2016412163511854.gif (370×345)

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

2016412163541130.gif (235×373)

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  > 
<textview  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  android:text="请您输入要保存的内容:" 
  /> 
 <edittext 
  android:id="@+id/addtext" 
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content" 
  android:hint="请您在此处输入文件内容!" 
 />   
 <button  
  android:id="@+id/addbutton" 
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content" 
  android:text="save" 
 /> 
 <button 
  android:id="@+id/showbutton" 
  android:layout_width="wrap_content"  
  android:layout_height="wrap_content" 
  android:text="show" 
 /> 
 <textview 
  android:id="@+id/showtext"  
  android:layout_width="fill_parent"  
  android:layout_height="wrap_content"  
  /> 
  
</linearlayout> 

activity代码

package cn.com.file; 
 
import java.io.bytearrayoutputstream; 
import java.io.fileinputstream; 
import java.io.filenotfoundexception; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
 
import android.app.activity; 
import android.os.bundle; 
import android.view.view; 
import android.view.view.onclicklistener; 
import android.widget.button; 
import android.widget.edittext; 
import android.widget.textview; 
import android.widget.toast; 
 
public class filetest extends activity { 
  private edittext edittext; 
  private textview showtextview; 
  // 要保存的文件名 
  private string filename = "chenzheng_java.txt"; 
 
  @override 
  public void oncreate(bundle savedinstancestate) { 
    super.oncreate(savedinstancestate); 
    setcontentview(r.layout.main); 
    // 获取页面中的组件 
    edittext = (edittext) findviewbyid(r.id.addtext); 
    showtextview = (textview) findviewbyid(r.id.showtext); 
    button addbutton = (button) this.findviewbyid(r.id.addbutton); 
    button showbutton = (button) this.findviewbyid(r.id.showbutton); 
    // 绑定单击事件 
    addbutton.setonclicklistener(listener); 
    showbutton.setonclicklistener(listener); 
 
  } 
 
  // 声明监听器 
  private view.onclicklistener listener = new onclicklistener() { 
    public void onclick(view v) { 
      button view = (button) v; 
      switch (view.getid()) { 
      case r.id.addbutton: 
        save(); 
        break; 
      case r.id.showbutton: 
        read(); 
        break; 
 
      } 
 
    } 
 
  }; 
 
  /** 
   *@author chenzheng_java 
   *保存用户输入的内容到文件 
   */ 
  private void save() { 
 
    string content = edittext.gettext().tostring(); 
    try { 
      /* 根据用户提供的文件名,以及文件的应用模式,打开一个输出流.文件不存系统会为你创建一个的, 
       * 至于为什么这个地方还有filenotfoundexception抛出,我也比较纳闷。在context中是这样定义的 
       *  public abstract fileoutputstream openfileoutput(string name, int mode) 
       *  throws filenotfoundexception; 
       * openfileoutput(string name, int mode); 
       * 第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名 
       *     该文件会被保存在/data/data/应用名称/files/chenzheng_java.txt 
       * 第二个参数,代表文件的操作模式 
       *     mode_private 私有(只能创建它的应用访问) 重复写入时会文件覆盖 
       *     mode_append 私有  重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件 
       *     mode_world_readable 公用 可读 
       *     mode_world_writeable 公用 可读写 
       * */ 
      fileoutputstream outputstream = openfileoutput(filename, 
          activity.mode_private); 
      outputstream.write(content.getbytes()); 
      outputstream.flush(); 
      outputstream.close(); 
      toast.maketext(filetest.this, "保存成功", toast.length_long).show(); 
    } catch (filenotfoundexception e) { 
      e.printstacktrace(); 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
 
  } 
 
  /** 
   * @author chenzheng_java 
   * 读取刚才用户保存的内容 
   */ 
  private void read() { 
    try { 
      fileinputstream inputstream = this.openfileinput(filename); 
      byte[] bytes = new byte[1024]; 
      bytearrayoutputstream arrayoutputstream = new bytearrayoutputstream(); 
      while (inputstream.read(bytes) != -1) { 
        arrayoutputstream.write(bytes, 0, bytes.length); 
      } 
      inputstream.close(); 
      arrayoutputstream.close(); 
      string content = new string(arrayoutputstream.tobytearray()); 
      showtextview.settext(content); 
 
    } catch (filenotfoundexception e) { 
      e.printstacktrace(); 
    } catch (ioexception e) { 
      e.printstacktrace(); 
    } 
 
  } 
 
} 

其他的都为默认。

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

2016412163634268.gif (776×235)

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网