今天花时间把android文件流那块学习了一下,写个小demo练习强化一下记忆。
文件的读写通过FileOutputStream和FileInputStream类来实现。功能:通过edittext输入文本,点击保存按钮保存到文件中,然后点击显示按钮将文本内容显示到textview上面
先来看下本人demo最终的效果吧:
布局很简单,只有一个EditText,两个按钮和一个TextView组成,就直接放代码了:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.administrator.mytestdemo.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:hint="please write something..." android:textSize="22sp" android:background="@drawable/et_shpe" android:padding="12dp"/> </LinearLayout> <Button android:id="@+id/save" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="22sp" android:text="保存" android:padding="8dp" android:background="@drawable/button_shape" android:layout_margin="16dp"/> <Button android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="22sp" android:text="显示" android:padding="8dp" android:background="@drawable/button_shape" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginBottom="16dp"/> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="22sp" android:text="There is nothing here." android:layout_margin="16dp" android:textColor="#251f1f" android:background="#addde2"/> </LinearLayout>其中控件的背景我都是用的drawable下定义的shape标签来实现的圆角效果。
下面来看下java具体实现代码吧:
1.首先定义的变量,其中用一个string类型表示文件名
private Button save,show; private EditText editText; private TextView textView; private String fileName="myf.txt";//创建的文件名2.找到控件,并对两个按钮进行监听
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); save= (Button) findViewById(R.id.save); show= (Button) findViewById(R.id.show); editText= (EditText) findViewById(R.id.edit); textView= (TextView) findViewById(R.id.text); save.setOnClickListener(this); show.setOnClickListener(this); }3.下面主要实现两个方法,一个是将输入框文本保存到文件,另一个是将文件的文本读取到textview上
/* 实现方法模块:默认在/data/data/应用目录下进行读写,采用私有模式 方法有两个: 1.infoSave()保存文本信息 2.infoShow()显示文本信息 */
//实现将输入框的内容保存到创建的文件中 private void infoSave(){ String writeContent=editText.getText().toString();//获取文本框中的内容 try { FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);//私有模式 byte[] bytes = writeContent.getBytes();//转化成字节流 fout.write(bytes);//将字节流写入 fout.close(); Toast.makeText(this,"success",Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
//实现将文件中的内容显示到TextView上 private void infoShow(){ String readContent=""; try { FileInputStream fileInputStream = openFileInput(fileName); int size=fileInputStream.available(); byte []buffer=new byte[size]; fileInputStream.read(buffer);//读取字节流 /* 将字节流转换为string类型的操作 */ readContent= EncodingUtils.getString(buffer,"UTF-8"); fileInputStream.close(); Toast.makeText(this,readContent,Toast.LENGTH_SHORT).show(); textView.setText(readContent);//将文件中的文本传给textview } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }4.为了方便理解,我把注释写的很清楚,没什么问题了,接下来就是按钮点击事件中的方法的调用:
@Override public void onClick(View v) { switch (v.getId()){ case R.id.save: infoSave();//保存文本信息 break; case R.id.show: infoShow();//显示文本信息 break; default: break; } }5.注意点:字节流转换成string型有很多方法,网上基本都能查到,但衡量一下,感觉还是本文中用EncodingUtils这个工具类直接一行代码就实现来得简单,该工具类在android studio中需要依赖相关库,可以在build.grade文件中添加
compile 'org.apache.httpcomponents:httpcore:4.4.4'
然后sync一下就ok
6.小结:
文件的使用千变万化,要注意相关方法的使用与实现途径;
文件的路径需要区分好:sd卡的路径与内置应用的路径(/mn/sd/..)(/data/data/包名/...)
后面可以拿一个文件管理器的小项目练练手,增强自己对于文件的掌握程度。
补充:笔者新手,本文章纯属记录日常所学和借鉴,如有不足和需要改进之处望大家提出。谢谢

本文介绍了一个简单的Android应用程序,演示如何使用FileOutputStream和FileInputStream进行文件的读写操作。通过EditText输入文本,点击按钮可以保存到文件或将文件内容显示到TextView。
780

被折叠的 条评论
为什么被折叠?



