首先我们来看一下MainActiviy
有时候我们经常通过javaSE的IO流来处理文件的读写,Android我们可以通过IO流与Activity绑定的方式进行读写操作
这时我们就可以通过openFileInput或openFileOutPut来处理。
文件存放的位置我们可以通过data/data/项目包名/files即可找到,前提是我们手机要进行rROOT
package com.example.administrator.day1; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; import org.apache.http.util.EncodingUtils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends Activity { private RadioGroup group; private RadioButton radio; private RadioButton radio1; private RadioButton radio2; private RadioButton radio3; private EditText con; private EditText fileName; private Button ser; private Button select; private FileOutputStream fileOutput; private BufferedOutputStream buffer; private FileInputStream fileRed; private BufferedInputStream bin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.openfile_layout); initView(); } public void initView(){//初始化控件方法及监听 group = (RadioGroup)findViewById(R.id.group); radio = (RadioButton)findViewById(R.id.radio1); radio1 = (RadioButton)findViewById(R.id.radio2); radio2 = (RadioButton)findViewById(R.id.radio3); radio3 = (RadioButton)findViewById(R.id.radio4); con = (EditText)findViewById(R.id.input); fileName = (EditText)findViewById(R.id.file_name); ser = (Button)findViewById(R.id.ser); select = (Button)findViewById(R.id.select); ser.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { writeText(fileName.getText().toString(), con.getText().toString());//写入操作 } }); select.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openFile(fileName.getText().toString());//查看操作 } }); } public void writeText(String fileName,String content){//写入方法 try { fileOutput = openFileOutput(fileName,MODE_PRIVATE);//以提供自己读写的私有方式打开该文件 buffer = new BufferedOutputStream(fileOutput); buffer.write(content.getBytes("UTF-8"));//写入字节流 }catch (IOException e){ e.printStackTrace(); }finally { if(buffer!=null){ try { buffer.close(); }catch (Exception e1){ e1.printStackTrace(); } if(fileOutput!=null){ try { fileOutput.close(); }catch (Exception e2){ e2.printStackTrace(); } } } } } public void openFile(String fileName){//读取方法 try { // String path =this.getFilesDir().getPath().toString()+"/"; fileRed = openFileInput(fileName); int lenght = fileRed.available(); bin = new BufferedInputStream(fileRed); byte by[] = new byte[lenght];//字节缓冲 if(bin.read(by)!=-1){//如果没有读取到末尾就正常显示 String contex = EncodingUtils.getString(by, "UTF-8"); con.setText(contex); } }catch (IOException e){ e.printStackTrace(); }finally { if(bin!=null){ try { bin.close(); }catch (Exception e){ e.printStackTrace(); } if(fileRed!=null){ try { fileRed.close(); }catch (Exception e2){ e2.printStackTrace(); } } } } } }
下面是我们的layout文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="300dp"> <EditText android:id="@+id/input" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000000"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:text="文件名" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/file_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2"/> <Button android:id="@+id/ser" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="保存"/> <Button android:id="@+id/select" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="查看"/> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="字体颜色" android:layout_marginTop="10dp" android:layout_marginLeft="10dp"/> <RadioGroup android:id="@+id/group" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radio1" android:text="黑色" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/radio2" android:text="红色" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/radio3" android:text="蓝色" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/radio4" android:text="绿色" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> </RadioGroup> </LinearLayout>
最后我们不要忘了加入权限