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 ...
随机推荐
- 3.Node.js 自定义微信菜单
文章目录: 1.Node.js 接入微信公众平台开发 2.Node.js access_token的获取.存储及更新 3.Node.js 自定义微信菜单 ...
- Python3.6_x86通过FFpmeg获取视频或音频的时长和分辨率
前言 前段时间公司在做流媒体服务,与许多厂家合作拿了许多视频过来,现在要对这些视频文件进行整理,通过特殊的编码排列,获取他们的时长以及分辨率,这里我遇到一个大坑,请往下面看. # -*- coding ...
- Elasticsearch和Kibana安装
Elasticsearch安装 Elasticsearch至少需要Java 8.在撰写本文时,建议你使用Oracle JDK版本1.8.0_131.Java安装因平台而异,所以在这里不再赘述.Orac ...
- Java之IO流详解
IO流 Input/Output 完成输入/输出 应用程序运行时——数据在内存中 ←→ 把数据写入硬盘(磁带) 内存中的数据不可持久保存的 输入:从外部存储器(硬盘.磁带.U盘)把数据读入内存. ...
- python——根据电子表格的数据自动查找文件
最近刚接触python,找点小任务来练练手,希望自己在实践中不断的锻炼自己解决问题的能力. 经理最近又布置了一个很繁琐的任务给我:有一项很重大的项目做完了,但是要过审计(反正就是类似的审批之类的事情) ...
- 将页面内容转换Pdf\Word\Excel格式
项目中用到了将邮件内容转换为Pdf.Word.Excel格式,做为邮件附件发送. 查了一些解决方案,走了一些弯路.以此代码记录下. 转换PDF需要下载NReco.PdfGenerator.dll 以下 ...
- Perl格式化输出
Perl格式化输出 问题阐述 有时我们需要大量的重复数据,使用手工易于出错及比较繁琐.抓取特征,可以使用Perl脚本轻松搞定. 输出数据格式 主要特点 随机数生成 格式化输出 序列递增 Perl脚本 ...
- 如何在前端模版引擎开发中避免使用eval函数
前段时间,想着自己写一个简单的模版引擎,便于自己平时开发demo时使用.于是根据自己对模版引擎的理解,定义自己的模版格式,然后,根据自己定义的格式,编写处理函数,将模版标签中的字符串,解析成可执行的字 ...
- 【JavaScript学习】-事件响应,让网页交互
什么是事件: JavaScript 创建动态页面.事件是可以被 JavaScript 侦测到的行为. 网页中的每个元素都可以产生某些可以触发 JavaScript 函数或程序的事件. 比如说,当用户单 ...
- httpClient 发送请求后解析流重用的问题(HttpEntity的重用:BufferedHttpEntity)
使用场景: 项目中使用httpClient发送一次http请求,以流的方式处理返回结果,开始发现返回的流只能使用一次,再次使用就会出错,后来看了一些解决方案,EntityUtils.consume(r ...