文件操作案例
来源:互联网 发布:仁和知柏地黄丸的功效 编辑:程序博客网 时间:2024/05/22 10:30
将EditText中输入的数据保存下来,点击按钮后再讲文件中的读取出来显示TextView中
MainActivity.class
package com.superxingyun.filedemo2;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class MainActivity extends AppCompatActivity { private EditText edit; private Button btn; private TextView text; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 edit = (EditText) findViewById(R.id.edit); btn = (Button) findViewById(R.id.btn); text = (TextView) findViewById(R.id.txt); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { WriteFiles(edit.getText().toString()); text.setText(readFiles()); } }); } //保存文件的内容 public void WriteFiles(String content){ try { FileOutputStream fos = openFileOutput("zzzzz.txt", MODE_PRIVATE);//创建文件以及其权限 fos.write(content.getBytes()); fos.close(); } catch (FileNotFoundException e){ e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //读取文件的内容 public String readFiles(){ String content = null; try { FileInputStream fis = openFileInput("zzzzz.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte buffer [] = new byte[1024]; int len = 0; while ((len = fis.read(buffer)) != -1){ baos.write(buffer, 0 ,len); } content = baos.toString(); fis.close(); baos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } return content; }}
如要想将文件放入外置SD卡中需要添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>activity_main.xml
<?xml version="1.0" encoding="utf-8"?><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="com.superxingyun.filedemo2.MainActivity"> <EditText android:id="@+id/edit" android:layout_width="match_parent" android:layout_height="150dp" android:layout_marginTop="15dp"/> <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/edit" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_marginTop="30dp" /> <TextView android:id="@+id/txt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="29dp" android:layout_below="@+id/btn" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /></RelativeLayout>
1 0
- java文件操作案例
- 文件操作案例
- C++文件操作典型案例
- 文件操作案例-大文件加解密
- 单个文件上传的案例操作
- 文件操作函数的案例分析
- android文件操作案例初学中遇到的问题
- C/C++学习之C提高-----文件操作,C程序读写文件、配置文件读写案例
- Nunit简单操作案例
- C# 操作内存案例
- Jquery Datepicker操作案例
- jquery操作案例版
- crm操作案例实体
- 数据库操作案例
- HighCharts操作案例-柱状图
- 【操作案例】CListCtrl
- LVM操作案例
- Thinkphp事务操作案例
- Java如何找出数组中前k个高频元素
- LeetCode[126.Word Ladder II]题解 难度[hard]
- Java 数组转换成List,然后执行add或remove抛异常UnsupportedOperationException问题的解决
- Struts 2.5.2 创建一个简单的项目--登录
- 如何实现servlet和struts的友好共存
- 文件操作案例
- 阅读郭林《第一行代码》的笔记——第7章 跨程序共享数据,探究内容提供器
- php字符串位运算的一个小技巧
- URDF文件构建中坐标系
- freeMarker生成word文档
- JSTL标签库之C标签的使用
- Sum of Two Integers
- SVN服务器搭建(Windows)
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'