AndroidStudio——数据存储之内部存储
来源:互联网 发布:淘宝二手书出售流程 编辑:程序博客网 时间:2024/04/28 14:54
InnerIoActivity布局:
public class InnerIoActivity extends AppCompatActivity { private Button save, read, delete; private EditText content; private TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_inner_io); save = (Button) findViewById(R.id.save); read = (Button) findViewById(R.id.read); delete = (Button) findViewById(R.id.delete); content = (EditText) findViewById(R.id.content); show = (TextView) findViewById(R.id.show); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 调用SAveFile方法 文件保存到date/date目录下 saveFile(); } }); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { show.setText(readFile()); } }); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { removeFile(); } }); } //删除文件 public void removeFile() { String[] files = fileList(); for (String str : files) { if (str.equals("text.text")) { deleteFile(str); break; } } } //从内存储中读取文件 public String readFile() { // BufferedReader包装流(字符流 字节流) 带缓冲区的 BufferedReader reader = null; FileInputStream fis = null; StringBuilder sbd = new StringBuilder(); try { fis = openFileInput("text.text"); reader = new BufferedReader(new InputStreamReader(fis)); sbd.append(getFilesDir().getCanonicalPath()); String row = ""; while ((row = reader.readLine()) != null) { sbd.append(row); } } catch (FileNotFoundException e) { Toast.makeText(getBaseContext(), "文件不存在", Toast.LENGTH_SHORT).show(); //e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return sbd.toString(); } //保存文件到内存储 public void saveFile() { FileOutputStream fos = null; try { /* openFileOutput返回一个输出字节流 指向的路径为data/data/包名/files 参数1:文件名称(如果不存在则自动创建) 参数2:模式MODE_APPEND文件内容可樶加 模式MODE_PRIVATE文件内容被覆盖 */ fos = openFileOutput("text.text", MODE_APPEND); String str = content.getText().toString(); fos.write(str.getBytes()); Toast.makeText(getBaseContext(), "保存成功", Toast.LENGTH_SHORT).show(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); fos.flush(); } catch (IOException e) { e.printStackTrace(); } } } }}
activity_inner_io布局文件:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.dell.jreduch008.InnerIoActivity"> <EditText android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入内容" android:layout_alignParentStart="true" android:layout_alignParentTop="true" /> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存" android:layout_below="@+id/content" android:layout_alignParentStart="true" /> <Button android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取" android:layout_above="@+id/show" android:layout_centerHorizontal="true" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/show" android:layout_below="@id/save"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" android:id="@+id/delete" android:layout_above="@+id/show" android:layout_alignParentEnd="true" /></RelativeLayout>
1 0
- AndroidStudio——数据存储之内部存储
- AndroidStudio——数据存储之SD卡存储
- Android数据存储之——内部存储
- 数据存储之内部存储
- AndroidStudio——数据存储(一)
- Android数据存储之内部存储
- 安卓数据保存之——内部存储
- 数据存储—读写内部存储的文件数据
- File存储—内部存储
- Android数据存储之内部存储、外部存储
- android数据存储方式之二 文件存储:内部存储
- Android 数据存储:五大存储之File存储(内部存储与外部存储)
- Android之内部存储读取数据
- Android--数据持久化之内部存储、Sdcard存储
- Android数据存储之内部文件存储(一)
- Android数据存储之Internal Storage内部存储
- Android数据存储方式——文件存储(内部和外部空间)
- android数据存储_内部存储
- BZOJ1083: [SCOI2005]繁忙的都市
- BZOJ1854: [Scoi2010]游戏
- BZOJ1724: [Usaco2006 Nov]Fence Repair 切割木板
- BZOJ1079: [SCOI2008]着色方案
- BZOJ1237: [SCOI2008]配对
- AndroidStudio——数据存储之内部存储
- BZOJ1112: [POI2008]砖块Klo
- 解决Android布局添加EditText之后无法预览的问题
- POJ 2728 Desert King 最优比率生成树
- DP——由蒟蒻到神犇的进阶之路
- jquery学习160815
- 《将博客搬至CSDN》
- 【codevs 1158】尼克的任务
- 大津法---OTSU算法