当前位置:K88软件开发文章中心编程语言APP编程Android01 → 文章内容

6.1 数据存储与访问之——文件存储读写

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-12 6:26:34

v) { switch (v.getId()) { case R.id.btnclean: editdetail.setText(""); editname.setText(""); break; case R.id.btnsave: FileHelper fHelper = new FileHelper(mContext); String filename = editname.getText().toString(); String filedetail = editdetail.getText().toString(); try { fHelper.save(filename, filedetail); Toast.makeText(getApplicationContext(), "数据写入成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "数据写入失败", Toast.LENGTH_SHORT).show(); } break; case R.id.btnread: String detail = ""; FileHelper fHelper2 = new FileHelper(getApplicationContext()); try { String fname = editname.getText().toString(); detail = fHelper2.read(fname); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show(); break; } }}4.读取SD卡上的文件读取流程图:代码示例:运行效果图:同样打开DDMS的File Explorer,在旧版本的系统上我们可以直接在mmt\sdcard上找到,但是新版本的就可能需要我们自己找找了,首先我们来到这个路径下:点开sdcard,但是没东西,我们继续找唠叨后面这个/storage/emulated/legacy下找:好吧,他又跳到别的地方去了,我们继续找/storage/shell/emilated/0果然找到了,我们在SD卡里生成的test.txt!导出到电脑看下里面的内容:嘿嘿,果然读写SD卡成功~接下来我们来看下代码是怎么写的:代码实现:main_activity.xml:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.jay.example.filedemo2.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清输入文件名" /> <EditText android:id="@+id/edittitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="文件名" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清输入文件内容" /> <EditText android:id="@+id/editdetail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="文件内容" /> <Button android:id="@+id/btnsave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存到SD卡" /> <Button android:id="@+id/btnclean" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="清空" /> <Button android:id="@+id/btnread" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取sd卡中的文件" /></LinearLayout>接着我们来写一个SD操作类:SDFileHelper.java/** * Created by Jay on 2015/9/1 0001. */public class SDFileHelper { private Context context; public SDFileHelper() { } public SDFileHelper(Context context) { super(); this.context = context; } //往SD卡写入文件的方法 public void savaFileToSD(String filename, String filecontent) throws Exception { //如果手机已插入sd卡,且app具有读写sd卡的权限 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename; //这里就不要用openFileOutput了,那个是往手机内存中写数据的 FileOutputStream output = new FileOutputStream(filename); output.write(filecontent.getBytes()); //将String字符串以字节流的形式写入到输出流中 output.close(); //关闭输出流 } else Toast.makeText(context, "SD卡不存在或者不可读写", Toast.LENGTH_SHORT).show(); } //读取SD卡中文件的方法 //定义读取文件的方法: public String readFromSD(String filename) throws IOException { StringBuilder sb = new StringBuilder(""); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { filename = Environment.getExternalStorageDirectory().getCanonicalPath() + "/" + filename; //打开文件输入流 FileInputStream input = new FileInputStream(filename); byte[] temp = new byte[1024]; int len = 0; //读取文件内容: while ((len = input.read(temp)) > 0) { sb.append(new String(temp, 0, len)); } //关闭输入流 input.close(); } return sb.toString(); }}接着MainActivity.java实现相关逻辑:p

上一页  [1] [2] [3]  下一页


6.1 数据存储与访问之——文件存储读写