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

}

}

});

}

}

android中file的使用实例的更多相关文章

  1. android中反射技术使用实例

    在计算机科学领域.反射是指一类应用,它们能够自描写叙述和自控制.也就是说,这类应用通过採用某种机制来实现对自己行为的描写叙述(self-representation)和监測(examination), ...

  2. android中asynctask的使用实例

    参考此blog写的非常的好http://www.cnblogs.com/devinzhang/archive/2012/02/13/2350070.html MainActivity.java imp ...

  3. Android中GridView的实现实例

    实现效果: activity文件代码: package com.tmacsky; import android.app.Activity; import android.os.Bundle; impo ...

  4. android中数据存储

    android中数据存储     Android 中存储数据的方式有五种:SQLite数据库.文件存储.内容提供者.网络.SharedPreferences(Key----value)五种存储方式. ...

  5. android中无限循环滑动的gallery实例

    android中无限循环滑动的gallery实例 1.点击图片有变暗的效果,使用imageview.setAlpha(),并且添加ontouchListener public void init() ...

  6. Android中制作自定义dialog对话框的实例

    http://www.jb51.net/article/83319.htm   这篇文章主要介绍了Android中制作自定义dialog对话框的实例分享,安卓自带的Dialog显然不够用,因而我们要继 ...

  7. Android中的跨进程通信方法实例及特点分析(二):ContentProvider

    1.ContentProvider简单介绍 在Android中有些数据(如通讯录.音频.视频文件等)是要供非常多应用程序使用的.为了更好地对外提供数据.Android系统给我们提供了Content P ...

  8. Android内核sysfs中switch类使用实例

    Android内核sysfs中switch类使用实例 最终在这个周末,能够干点自己想要干的事了. 由我这个二流的内核驱动开发人员来解析一下sysfs中的switch类.先猜測一下来历,在普通的嵌入式L ...

  9. 在Android Native层中创建Java虚拟机实例

    前言 Android应用中JNI代码,是作为本地方法运行的.而大部分情况下,这些JNI方法均需要传递Dalvik虚拟机实例作为第一个参数.例如,你需要用虚拟机实例来创建jstring和其他的Java对 ...

随机推荐

  1. 收货MIGO

    FUNCTION zrfc_mm003. *"---------------------------------------------------------------------- * ...

  2. 1352 - Colored Cubes (枚举方法)

    There are several colored cubes. All of them are of the same size but they may be colored differentl ...

  3. Swift - 获取屏幕点击坐标下所有对象(SpriteKit游戏开发)

    对于场景内对象元件的点击响应,我们可以在场景的touchesBegan()方法中内统一处理. SKScene中touchesBegan()是响应屏幕点击的方法,在这里面我们可以先获取点击位置下所有的对 ...

  4. 使用高德地图API

    http://lbs.amap.com/smart/map/developer/mode/ 简单的附上一张图,其实能做得比这个更强大.

  5. 内省(二)之BeanUtils工具类

    上一篇内省(Introspector)讲到的是采用JavaAPI中的类来操作bean及其属性,而Apache也开源了第三方框架来简化和丰富了对bean属性的操作,这个框架就是BeanUtils. 使用 ...

  6. Python 链接MysqlDB

    下载安装MySQLdb <1>linux版本 http://sourceforge.net/projects/mysql-python/ 下载,在安装是要先安装setuptools,然后在 ...

  7. hdc和hwnd的区别

    句柄概念在WINDOWS编程中是一个很重要的概念,在许多地方都扮演着重要的角色.但由此而产生的句柄概念也大同小异,比如:<<Microsoft   Windows   3   Develo ...

  8. 树莓派学习笔记——使用文件IO操作GPIO SysFs方式

    0 前言     本文描写叙述假设通过文件IO sysfs方式控制树莓派 GPIO端口.通过sysfs方式控制GPIO,先訪问/sys/class/gpio文件夹,向export文件写入GPIO编号, ...

  9. 全局忽略编译警告(设置QMAKE_CXXFLAGS )

    msvc编译器从2010 sp1开始就已经支持UTF-8的源码文件了,然后到vs2012又不支持了,官方表示是BUG.到目前最新的vs2013就解决了这个问题... 但是在编译时仍然会出现4819的警 ...

  10. 暂停和屏蔽右键网页中的Flash

    如何暂停网页中的Flash?原理很简单,就是屏蔽Flash的消息即可.屏蔽右键也可以通过此方法 直接贴代码吧,加了注释,很容易就能懂了 新建工程,加一个WebBrowser,再加两个按钮.Flash ...