android中file的使用实例

来源:互联网 发布:天猫美工工作描述 编辑:程序博客网 时间:2024/06/05 18:11

File是android的4种存储方式的一种。File就是文件的意思一个文件,你无非是想进行读写操作。所以这就用到两个流。一个数输入流,一个是输出流。FileOutstream,和FileInputSream。这两个和java基础的流是一样的。如果你对流不清楚可以我看看我以前写的java对流的使用。

 

下面我写一个android程序对file是的使用

布局

 

下面我就写一个actitvty的代码

package com.android.app;

 

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

 

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

import android.widget.TextView;

 

public class File extends Activity {

    /** Called when the activity is firstcreated. */

    private EditTextcontent;

    private TextViewreadContent;

    private ButtonwriteButton;

    private ButtonreadButton;

    private CheckBoxcheckBox;

    private TextViewshowContent;

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.main);

       //绑定所有组件

       content = (EditText) findViewById(R.id.EditText01);

       readButton = (Button) findViewById(R.id.Button02);

       writeButton = (Button) findViewById(R.id.Button01);

       checkBox = (CheckBox) findViewById(R.id.CheckBox01);

       showContent = (TextView) findViewById(R.id.TextView01);

       readContent = (TextView) findViewById(R.id.TextView02);

 

      

       //点击写按钮触发写入事件

       writeButton.setOnClickListener(new OnClickListener() {

 

           @Override

           public void onClick(View v) {

              FileOutputStream fos = null;

              try {

                  if (checkBox.isChecked()) {

                     //这个就是一个流文件的写入第一个参数是一个文件的文件的文件名,后面的文件的写入方式

                     fos = openFileOutput("fish", Context.MODE_APPEND);//追加的写入

                  } else {

                     fos = openFileOutput("fish", Context.MODE_PRIVATE);//覆盖的写入

                  }

 

                  String text = content.getText().toString();

                  fos.write(text.getBytes());   //将你输入的东西写入缓冲区。这里还没有在文件里面写

                  showContent.setText("写入成功");

                  content.setText("");

 

              } catch (FileNotFoundException e) {

                  // TODO Auto-generatedcatch block

                  e.printStackTrace();

              } catch (IOException e) {

                  // TODO Auto-generatedcatch block

                  e.printStackTrace();

              } finally {

                  if (fos !=null) {

                     try {

                         fos.flush();//当执行完这个你的文件里面才有你写入的内容

                         fos.close();

                     } catch (IOException e) {

                         // TODO Auto-generatedcatch block

                         e.printStackTrace();

                     }

 

                  }

              }

           }

       });

      

      

       //点击读按钮触发读入事件

       readButton.setOnClickListener(new OnClickListener() {

 

           @Override

           public void onClick(View v) {

              readContent.setText("");

              FileInputStream fis = null;

              try {

                  fis = openFileInput("fish");

                  if (fis.available() == 0) {

                     return;

                  } else {

                     byte[] con =new byte[fis.available()];

                     while (fis.read(con) != -1) {

                     }

                     readContent.setText(new String(con));

                      showContent.setText("读取成功");

                  }

              } catch (FileNotFoundException e) {

                  // TODO Auto-generatedcatch block

                  e.printStackTrace();

              } catch (IOException e) {

                  // TODO Auto-generatedcatch block

                  e.printStackTrace();

              }

 

           }

       });

 

    }

}

原创粉丝点击