Android - 读取文件存储的数据
存取手机中的文件数据。
写入和读取的操作格式均为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 - 读取文件存储的数据的更多相关文章
- Android使用文件存储数据
Android上最基本的存储数据的方式即为使用文件存储数据,使用基本的Java的FileOutStream,BufferedWriter,FileInputStream和BufferedReader即 ...
- android之文件存储和读取
一.权限问题 手机中存储空间分为ROM和SDcard,ROM中放着操作系统以及我们安装的APP,而sdcard中一般放置着我们APP产生的数据.当然,Android也为每个APP在ROM中创建一个数据 ...
- Android学习——文件存储
在Andriod开发中,文件存储和Java的文件存储类似.但需要注意的是,为了防止产生碎片垃圾,在创建文件时,要尽量使用系统给出的函数进行创建,这样当APP被卸载后,系统可以将这些文件统一删除掉.获取 ...
- Android File文件存储功能
1.介绍 2.使用方法 3.文件存储位置 4.java后台代码 package com.lucky.test47file; import android.support.v7.app.AppCompa ...
- 19.Android之文件存储方法学习
Android开发中会用到文件存储,今天来学习下. 先改下布局界面: <?xml version="1.0" encoding="utf-8"?> ...
- android 开发-文件存储之读写sdcard
android提供对可移除的外部存储进行文件存储.在对外部sdcard进行调用的时候首先要调用Environment.getExternalStorageState()检查sdcard的可用状态.通过 ...
- C语言 读取文件中特定数据
//读取文件数据 #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> struct jia ...
- HDFS读文件过程分析:读取文件的Block数据
转自http://shiyanjun.cn/archives/962.html 我们可以从java.io.InputStream类中看到,抽象出一个read方法,用来读取已经打开的InputStrea ...
- C语言:字符串读取流读取文件中的数据
#include<stdio.h> int main() { //定义文件指针 FILE *f = NULL; //打开文件 f = fopen("1.txt",&qu ...
随机推荐
- Java之JSON数据
特别注意:使用JSON前需要导包 操作步骤地址:http://blog.csdn.net/baidu_37107022/article/details/70876993 1.定义 JSON(JavaS ...
- HTML5 Web Worker简单使用
Web Workers 是 HTML5 提供的一个javascript多线程解决方案,我们可以将一些大计算量的代码交由web Worker运行而不冻结用户界面. 一:如何使用Worker Web Wo ...
- C#中switch的使用
今天在网上看到有人给出这么一个程序需求,博主就拿来回忆回忆C#中switch的用法 程序需求如下: 根据星期几(一 ~日) ,输出特价菜“一”.“二”.“三”,输出“干煸扁豆6元.”“四”.“五”,输 ...
- linux 下配置 MAVEN
1.下载maven http://maven.apache.org/download.cgi 2.解压 tar xzvf apache-maven-3.1.0.tar.gz 3.配置环境变量 sud ...
- 解密SuperWebview的一种另类方法
解密SuperWebview的一种另类方法 什么是SuperWebview SuperWebview是APICloud官方推出的另一项重量级API生态产品,以SDK方式提供,致力于提升和改善移动设备W ...
- oracle学习笔记(1)-三级模式SCHEMA
oracle三级模式及二级映像 模式(schema)是数据库的一个名词,大部分的数据库在结构上都有三级模式的特征,了解下基本的概念,有助于后续深入的学习. 用老罗坚果pro发布会的话说就是,不罗嗦,先 ...
- Python 编码错误的本质原因
转载自:https://foofish.net/python-unicode-error.html 不论你是有着多年经验的 Python 老司机还是刚入门 Python 不久的新贵,你一定遇到过Uni ...
- php中查询mysql如何在IN array中用
假如有一个数组 $arr = array(1,3,5,7,9)那么我在如何在php中使用mysqlWHERE id IN (1,3,5,7,9.......)$arr_string = join(', ...
- Tp框架获取客户端IP地址
/** * 获取客户端IP地址 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @return mixed */ function get_cl ...
- [leetcode-515-Find Largest Value in Each Tree Row]
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 / \ ...