Android编程中File文件常见存储与读取操作demo示例

作者:ITzhongzi 时间:2021-11-24 18:33:20 

本文实例讲述了Android编程中File文件常见存储与读取操作。分享给大家供大家参考,具体如下:

MainActivity文件代码如下:


package example.com.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class MainActivity extends Activity
{
 final String FILE_NAME = "test.txt";
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   System.out.println(new StringBuilder("a").append("b").append("c")
       .toString());
   // 获取两个按钮
   Button read = (Button) findViewById(R.id.read);
   Button write = (Button) findViewById(R.id.write);
   // 获取两个文本框
   final EditText edit1 = (EditText) findViewById(R.id.edit1);
   final EditText edit2 = (EditText) findViewById(R.id.edit2);
   // 为write按钮绑定事件 *
   write.setOnClickListener(new View.OnClickListener()
   {
     @Override
     public void onClick(View source)
     {
       // 将edit1中的内容写入文件中
       write(edit1.getText().toString());
       edit1.setText("");
     }
   });
   read.setOnClickListener(new View.OnClickListener()
   {
     @Override
     public void onClick(View v)
     {
       // 读取指定文件中的内容,并显示出来
       edit2.setText(read());
     }
   });
 }
 private String read()
 {
   try
   {
     // 打开文件输入流
     FileInputStream fis = openFileInput(FILE_NAME);
     byte[] buff = new byte[1024];
     int hasRead = 0;
     StringBuilder sb = new StringBuilder("");
     while ((hasRead = fis.read(buff)) > 0)
     {
       sb.append(new String(buff, 0, hasRead));
     }
     return sb.toString();
   }
   catch (Exception e)
   {
     e.printStackTrace();
   }
   return null;
 }
 private void write(String content)
 {
   try
   {
     // 以追加模式打开文件输出流
     FileOutputStream fos = openFileOutput(FILE_NAME, MODE_APPEND);
     // 将FileOutputStream包装成PrintStream
     PrintStream ps = new PrintStream(fos);
     // 输出文件内容
     ps.println(content);
     ps.close();
   }
   catch (Exception e)
   {
     e.printStackTrace();
   }
 }
}

布局文件代码如下:


<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <EditText
   android:id="@+id/edit1"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:lines="4"/>
   <Button
     android:id="@+id/write"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="write"/>
 <EditText
   android:id="@+id/edit2"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:editable="false"
   android:lines="4"/>
 <Button
   android:id="@+id/read"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="read"/>
 </LinearLayout>

希望本文所述对大家Android程序设计有所帮助。

来源:http://blog.csdn.net/itzhongzi/article/details/52243362

标签:Android,File,文件
0
投稿

猜你喜欢

  • Mybatis如何传入多个参数的实现代码

    2022-02-26 02:52:24
  • idea中同一SpringBoot项目多端口启动

    2023-05-09 06:39:27
  • SpringBoot如何进行参数校验实例详解

    2022-10-24 03:39:33
  • Java四种访问控制修饰符知识点总结

    2021-08-12 11:40:43
  • 每日六道java新手入门面试题,通往自由的道路--多线程

    2022-12-13 16:04:07
  • C# String Replace高效的实例方法

    2023-10-26 23:24:08
  • 详解JAVA 弱引用

    2022-03-12 01:30:29
  • java实现将结果集封装到List中的方法

    2021-10-27 22:29:45
  • C#并发编程入门教程之概述

    2022-08-08 09:44:23
  • C#byte数组传入C操作方法

    2021-11-06 12:30:18
  • MyBatis深入解读懒加载的实现

    2021-10-02 01:09:44
  • File.createTempFile创建临时文件的示例详解

    2022-05-03 08:52:41
  • SpringBoot整合mybatis的方法详解

    2023-09-02 06:23:57
  • Java实现简单员工管理系统

    2021-12-13 17:51:26
  • Java多线程run方法中直接调用service业务类应注意的问题及解决

    2021-12-28 19:51:46
  • Java使用excel工具类导出对象功能示例

    2022-07-04 23:06:50
  • JAVA宝藏工具hutool的使用

    2023-08-25 20:20:12
  • C# 添加、修改以及删除Excel迷你图表的实现方法

    2023-04-06 22:07:27
  • Nacos入门过程的坑--获取不到配置的值问题

    2023-07-24 11:39:41
  • Jackson序列化和反序列化忽略字段操作

    2022-08-29 14:01:14
  • asp之家 软件编程 m.aspxhome.com