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. [转]java-Three Rules for Effective Exception Handling

    主要讲java中处理异常的三个原则: 原文链接:https://today.java.net/pub/a/today/2003/12/04/exceptions.html Exceptions in ...

  2. EasyUI - 后台管理系统 - 增加,删除,修改

    效果: html代码: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ad ...

  3. Spring MVC程序

    Spring MVC程序(IDEA开发环境)   回顾Java平台上Web开发历程来看,从Servlet出现开始,到JSP繁盛一时,然后是Servlet+JSP时代,最后演化为现在Web开发框架盛行的 ...

  4. Qt 多线程 详细函数说明及其事例

    转:http://www.cnblogs.com/hicjiajia/archive/2011/02/03/1948955.html Qt线程类 Qt 包含下面一些线程相关的类:QThread 提供了 ...

  5. ajax表单提交全路径

    //ajax提交form表单的方式 $(document).ready(function() { $('#shopping-submit').click(function() { alert(&quo ...

  6. [转载]关于网传JDK1.7语法层次支持集合的问题

    以  JDK1.7新特性 为关键词进行百度的话,总能发现这样的描述,说: 从语法层面上支持集合,不再是数组的专利.还有这样的例子: final List<Integer> piDigits ...

  7. Windows Azure 安全最佳实践 - 第 5 部分:基于Claim 的标识,单点登录

    基于Claim的身份标识是处理网站与 Web 服务的身份认证和访问一种简单而强大的方式,无论您是在本地工作还是面向云工作.您可以通过减少自定义实施和使用基于Claim的单一简化标识模型,创建更安全的应 ...

  8. 好多NFS的文章

    http://www.cnblogs.com/lidabo/category/587288.html http://www.cnblogs.com/lidabo/p/4380555.html

  9. 【ASP.NET Web API教程】2.1 创建支持CRUD操作的Web API

    原文 [ASP.NET Web API教程]2.1 创建支持CRUD操作的Web API 2.1 Creating a Web API that Supports CRUD Operations2.1 ...

  10. Maven插件之portable-config-maven-plugin(不同环境打包)

    在大型的项目组中,分不同的开发环境,测试环境,生产环境(说白了就是配置文件不同,或者数据源,或者服务器,或者数据库等);问题来了,如何使用Maven针对不同的环境来打包呢? Maven提供了Profi ...