自带内存上的读写(openFileOutput和openFileInput)

自带内存上的读写(openFileOutput和openFileInput)

效果图示例

 

自带内存上的读写(openFileOutput和openFileInput)_第1张图片

自带内存上的读写(openFileOutput和openFileInput)_第2张图片

自带内存上的读写(openFileOutput和openFileInput)_第3张图片

 

//这是在手机自带内存上的读和写操作 -- 数据保存在data/data/包名/

//和SharePeferences读和写一样都是在自带内存上操作的,而SharePeferences利用编辑器进行读写操作

//这个功能是利用openFileOutput和openFileInput进行写和读操作 -- 相同点:都是在自带内存上操作

 

 

============================

 

1、activity_main.xml布局文件

 

代码

 

<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="${relativePackage}.${activityClass}" >

    <LinearLayout
        android:id="@+id/one_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edit_filename"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:hint="文件名" />

        <Button
            android:id="@+id/bt_sava"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="保存"
            android:onClick="sava"/>

        <Button
            android:id="@+id/bt_read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="读取"
            android:onClick="read" />
    </LinearLayout>

    <EditText
        android:id="@+id/edit_filecontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/one_layout"
        android:background="#000"
        android:hint="文件内容"
        android:textColor="#fff" />

</RelativeLayout>

 

--------------------------------

 

 

2、菜单menu布局的main_menu.xml布局文件

代码

 

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/selected"
        android:title="选择文件列表"/>

</menu>

 

 

==============================

 

 

3、MainActivity 类

 

代码

 

public class MainActivity extends Activity {

 private EditText edit_filename;
 private EditText edit_filecontent;
 private ArrayAdapter<String> adapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  this.edit_filename = (EditText) this.findViewById(R.id.edit_filename);
  this.edit_filecontent = (EditText) this.findViewById(R.id.edit_filecontent);
  
 }
 
 //创建一个静态选择菜单
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main_menu, menu);
  return super.onCreateOptionsMenu(menu);
 }
 //菜单的点击 事件 监听
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  if(item.getItemId() == R.id.selected){
   //弹出一个选择要读的文件名 列表框
   Dialog();
  //获取当前文件名 列表

//利用fileList()方法来读取内存里的文件

  adapter.addAll(fileList());
  }
  return super.onOptionsItemSelected(item);
 }
 
 private void Dialog() {
  //当点击菜单 列表 时 创建一个 选择文件列表框
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    //设置 框的 属性
    builder.setTitle("选择要读取的文件名");
    builder.setIcon(R.drawable.ic_launcher);
    //new一个适配器 -- 该适配器 还没有内容
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
    //把文件名 添加到 列表框
    builder.setAdapter(adapter, new OnClickListener() {
     
     @Override
     public void onClick(DialogInterface dialog, int which) {
      String filename = adapter.getItem(which);
      edit_filename.setText(filename);
      //把选择的文件名 读取内容
      read(null);
     }
    });
    builder.show();
 }

 //保存文件
 public void sava(View view){
  //获取输入的 文件名
  String file_name = edit_filename.getText().toString().trim();
  //获取edit里的文件的 内容
  String file_content = edit_filecontent.getText().toString().trim();
  //把获得的 文件名 和文件内容 保存到 内存
  try {
   //第一个参数 -- 要保存的文件名
   //第二个参数 -- 写的模式
   OutputStream os = openFileOutput(file_name, Context.MODE_PRIVATE);
   //把需要的数据 写入内存
   os.write(file_content.getBytes());
   //提示
   Toast.makeText(this, "保存成功!", Toast.LENGTH_SHORT).show();
  
   os.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 //读取保存的文件
 public void read(View view){
  //获取要读的文件名
  String file_name = edit_filename.getText().toString().trim();
  //打开 要读的文件名
  try {
   InputStream is = openFileInput(file_name);
   byte[] b_read = new byte[is.available()];//文件的长度 粗略
   is.read(b_read);
   //把读完的 文件内容 显示到 edit 编辑框
   edit_filecontent.setText(new String(b_read));
   is.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

你可能感兴趣的:(自带内存上的读写(openFileOutput和openFileInput))