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 EditText content;

private TextView readContent;

private Button writeButton;

private Button readButton;

private CheckBox checkBox;

private TextView showContent;

@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();

}

}

});

}

}

更多相关文章

  1. android 编译环境
  2. Android(安卓)PreferenceActivity 学习笔记
  3. ADB常用操作
  4. 【android编程】 第二讲-这是好的一个开始
  5. Android(安卓)数据存储---SharedPreference
  6. android录音MP3格式文件
  7. Android开发―随笔杂记(2010年的)
  8. android 性能检测资料合集
  9. Eclipse中android插件的安装

随机推荐

  1. 提供一些Android免费课程分享给大家
  2. Android(安卓)OpenGL例子学习
  3. 在AndroidManifest.xml文件中的android:w
  4. android获取短信所有内容
  5. Android: 系统默认音量
  6. Android逆向分析常用网站
  7. Android中常用的Intent.Action整理
  8. android(2.0以后版本) 中读取联系人和通
  9. 更新SDK后eclipse报错This Android(安卓)
  10. Android(安卓)NDK 开发