Android访问数据文件
来源:互联网 发布:c语言求1到n阶乘的和 编辑:程序博客网 时间:2024/05/16 19:33
首先我们来看一下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>
最后我们不要忘了加入权限
0 0
- Android访问数据文件
- Android数据文件的访问权限
- ADO访问文本数据文件
- 如何查看android数据文件?
- Android数据文件存储路径
- 分布数据文件访问多个磁盘
- Android 操作 外部导入数据文件
- 数据文件
- 在DELPHI中使用ADO直接访问Excel数据文件
- 在DELPHI中使用ADO直接访问Excel数据文件
- 在DELPHI中使用ADO直接访问Excel数据文件
- Mapx 属性数据文件用oledb方式访问的问题
- DELPHI中使用ADO直接访问Excel数据文件
- MapReduce中如何访问外部jar包和数据文件
- 误操作,将数据文件物理删除,访问不了数据库,解决方法
- Android中数据文件解析(Json解析)
- Android中数据文件解析(Json解析)
- Android 自定义SQlite数据文件存放位置
- CButtonST使用技巧
- Balanced Binary Tree 判断是否为平衡二叉树
- swift 调用 C语言库
- 阅读程序
- CSS3变形之2D变形
- Android访问数据文件
- 阅读程序
- 写给准爸爸的专业指导
- 海底机器人问题(最小费用流)
- Python学习-map&set
- android中的资源,资源与xml文件
- 第16周阅读程序2(1)
- DHCP报文及其格式
- 根据类的名字String生成相应的类