存取手机中的文件数据。

写入和读取的操作格式均为UTF-8。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private String filename = "test";
	private TextView show;
	private EditText et;
	File sdcard = Environment.getExternalStorageDirectory();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		et = (EditText) findViewById(R.id.et);
		show = (TextView) findViewById(R.id.show);

		findViewById(R.id.writeBtn).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
					OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
					osw.write(et.getText().toString());
					osw.flush();
					fos.flush();
					osw.close();
					fos.close();
					Toast.makeText(getApplicationContext(), "写入完成", Toast.LENGTH_SHORT).show();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

		findViewById(R.id.readBtn).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					FileInputStream fis = openFileInput(filename);
					InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
					char input[] = new char[fis.available()];
					isr.read(input);
					isr.close();
					fis.close();
					String readed = new String(input);
					show.setText(readed);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

		findViewById(R.id.writeBtn1).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				//首先要获得当前sd卡的工作目录
				File myfile = new File(sdcard,"This is my file.txt");
				if(!sdcard.exists()){
					Toast.makeText(getApplicationContext(), "there is no sdcard", Toast.LENGTH_SHORT).show();
					return;
				}
				try {
					myfile.createNewFile();
					FileOutputStream fos = new FileOutputStream(myfile);
					OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
					osw.write(et.getText().toString());
					osw.flush();
					osw.close();
					fos.close();
					Toast.makeText(getApplicationContext(), "文件已创建完成", Toast.LENGTH_SHORT).show();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		findViewById(R.id.readBtn1).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				File myfile = new File(sdcard,"This is my file.txt");
				if(myfile.exists()){
					try {
						FileInputStream fis = new FileInputStream(myfile);
						InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
						char[] input = new char[fis.available()];
						isr.read(input);
						isr.close();
						fis.close();
						String inString = new String(input);
						show.setText(inString);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		});
	}

}

布局文件:

放入1个EditText,4个button,1个textview

<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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.rust.readfiletest.MainActivity" >

    <EditText
        android:id="@+id/et"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

        android:hint="enter" />

    <Button
        android:id="@+id/writeBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et"
        android:text="save" />

    <Button
        android:id="@+id/readBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et"
        android:layout_toRightOf="@id/writeBtn"
        android:text="read" />

    <Button
        android:id="@+id/writeBtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et"
        android:layout_toRightOf="@id/readBtn"
        android:text="write e" />

    <Button
        android:id="@+id/readBtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/et"
        android:layout_toRightOf="@id/writeBtn1"
        android:text="read e" />

    <TextView
        android:id="@+id/show"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/writeBtn"
        />

</RelativeLayout>

Android - 读取文件存储的数据的更多相关文章

  1. Android使用文件存储数据

    Android上最基本的存储数据的方式即为使用文件存储数据,使用基本的Java的FileOutStream,BufferedWriter,FileInputStream和BufferedReader即 ...

  2. android之文件存储和读取

    一.权限问题 手机中存储空间分为ROM和SDcard,ROM中放着操作系统以及我们安装的APP,而sdcard中一般放置着我们APP产生的数据.当然,Android也为每个APP在ROM中创建一个数据 ...

  3. Android学习——文件存储

    在Andriod开发中,文件存储和Java的文件存储类似.但需要注意的是,为了防止产生碎片垃圾,在创建文件时,要尽量使用系统给出的函数进行创建,这样当APP被卸载后,系统可以将这些文件统一删除掉.获取 ...

  4. Android File文件存储功能

    1.介绍 2.使用方法 3.文件存储位置 4.java后台代码 package com.lucky.test47file; import android.support.v7.app.AppCompa ...

  5. 19.Android之文件存储方法学习

    Android开发中会用到文件存储,今天来学习下. 先改下布局界面: <?xml version="1.0" encoding="utf-8"?> ...

  6. android 开发-文件存储之读写sdcard

    android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...

  7. C语言 读取文件中特定数据

    //读取文件数据 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct jia ...

  8. HDFS读文件过程分析:读取文件的Block数据

    转自http://shiyanjun.cn/archives/962.html 我们可以从java.io.InputStream类中看到,抽象出一个read方法,用来读取已经打开的InputStrea ...

  9. C语言:字符串读取流读取文件中的数据

    #include<stdio.h> int main() { //定义文件指针 FILE *f = NULL; //打开文件 f = fopen("1.txt",&qu ...

随机推荐

  1. 【小练习06】HTML+CSS--电影公告

    要求实现如下效果图: 代码演示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...

  2. 一个web应用的诞生(13)--冲向云端

    有句话叫所有的乐趣都在部署之前,也许这个小应用还有很多缺陷,也许它还不够完美,但是,仔细想想,其实没有什么能比自己的网站在互联网中上线更令人满足的了,但是满足的背后,总是存在着很多的风险,以至于几乎所 ...

  3. Android Studio 工具栏添加常用按钮

    本文中 Android Studio 的版本为 android Studio 2.2 ,操作系统为 Windows,如有操作不同,可能是版本差异.在工具栏中添加一些常用的按钮,如设置.DDMS,有利于 ...

  4. HTML中那些不常用标签

    先思考一个问题:为什么H5里面又多了那么多看似没用的标签? 我们知道,<div>能干百分之99的标签能干的事,而标签的主要作用是用来包裹内容,只要把基本内容都包含进去不就好了??胡闹!不带 ...

  5. 有关苹果无法导出p12证书的问题解决办法。

    原因一 所选类型选择错误.解决办法:左侧有两个分类,一个是钥匙串,一个是种类,要选择种类中的我的证书或者证书.然后在右侧证书列表中,右键导出即可. 原因二 使用钥匙串生成的证书有问题,格式为(cert ...

  6. Android Studio常用快捷键使用

    以下是我在编程中实际用上的Android Studio快捷键,基于Windows系统,在使用过程中会不断添加不断完善,OSX版本的在另外一篇博客 Ctrl+Alt+L 格式化代码,编写完成项目来一下, ...

  7. IOS机型margin属性无效问题

    这个问题很多小伙伴都有遇到,这次本猿也遇到了,记录一笔. 结论:简单的解决办法margin换padding!如有误,请大神不吝赐教.在Chrome浏览器移动端测试iphone5,打开的也是没问题的(这 ...

  8. R语言统计分析技术研究 特征值选择技术要点

    特征值选择技术要点                          作者:王立敏 文章来源:  网络 1.特征值 特征值是线性代数中的一个重要概念.在数学,物理学,化学,计算机等领域有着广泛的应用. ...

  9. [iOS]从零开始开发一个即时通讯APP

    前言 这是我的毕业设计.刚开始确定这个课题的时候是因为以前有稍微研究过一些XMPP协议,在这个基础上做起来应该不难.然后开始选技术的时候还有半年,我想为什么不从更底层做起呢!那就不用XMPP,当时接触 ...

  10. 谈谈java中遍历Map的几种方法

    java中的map遍历有多种方法,从最早的Iterator,到java5支持的foreach,再到java8 Lambda,让我们一起来看下具体的用法以及各自的优缺点 先初始化一个map public ...