Android学习之 文件存储
来源:互联网 发布:淘宝客服管理软件 编辑:程序博客网 时间:2024/06/06 02:55
首先给大家介绍使用文件如何对数据进行存储,Activity提供了openFileOutput()方法可以用于把数据输出到文件中。
直接贴代码:
布局文件:
<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: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=".MainActivity" > <TextView android:id="@+id/showmsg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/showinfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_below="@id/showmsg" /></RelativeLayout>
MainActivity.java
运行效果展示:package com.example.filestorage;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintStream;import java.util.Scanner;import org.apache.http.util.ByteArrayBuffer;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {//接收文件输出对象FileOutputStream output=null;//写入文件对象FileInputStream input=null;private TextView showmsg;private TextView showinfo;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showmsg=(TextView) findViewById(R.id.showmsg);showinfo=(TextView) findViewById(R.id.showinfo);//将内容保存到文件try {output=super.openFileOutput("ha.txt", MainActivity.MODE_PRIVATE);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}PrintStream print=new PrintStream(output);print.println("姓名:Android");print.println(" ");print.println("年龄:20");print.println(" ");print.println("电话:110");print.close();//从文件中读入内容try {input=super.openFileInput("ha.txt");ByteArrayOutputStream stream=new ByteArrayOutputStream();byte[] buffer=new byte[1024]; int length=-1; while((length=input.read(buffer))!=-1) { stream.write(buffer,0,length); } stream.close(); showmsg.setText("个人信息"+"\n"+stream.toString()); input.close(); //第二种方式读取文件内容 input=super.openFileInput("ha.txt"); Scanner scanner=new Scanner(input); showinfo.setText("个人信息"); while (scanner.hasNext()) { showinfo.append("\n"); showinfo.append(scanner.next()+"\n");} scanner.close(); input.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
@author:www.lelexie.com 本文来自:恋人SEO www.loverseo.com
- Android学习之 文件存储
- Android学习之文件存储
- Android学习之文件存储方式
- Android学习14——数据存储之文件存储
- Android学习 数据存储之_文件存储
- Android 存储学习之在内部存储中读写文件
- Android 存储学习之在外部存储中读写文件
- Android文件存储学习
- Android 文件存储学习
- Android之文件存储
- android--存储之文件
- Android之文件存储
- android之文件存储
- Android 存储学习之使用SharedPreference保存文件
- 【Android】android存储篇之 文件存储
- Android数据存储之文件存储
- Android数据存储之文件存储
- Android数据存储之文件存储
- 学习目录
- Response.Redirect在新窗口打开
- android图片上传服务器
- Apache配置详解(最好的APACHE配置教程)
- SQL2005附加数据库,提示 数据库 版本为655,无法打开,此服务器支持611版...
- Android学习之 文件存储
- 压缩感知和稀疏表示的经典文献
- 宇龙酷派笔试题 2014 武汉
- OpenCV学习--腐蚀与膨胀
- linux下网络常用命令
- 创业方向--微信订餐
- LogBack学习记录(四)appender(输出源)的使用
- Centos安装g++
- android中Toast重复显示问题解决