zoukankan      html  css  js  c++  java
  • Android File数据存储

     1 <?xml version="1.0" encoding="utf-8"?>
    2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:layout_width="fill_parent"
    4 android:layout_height="fill_parent"
    5 android:orientation="vertical" >
    6 <EditText
    7 android:id="@+id/write_edit"
    8 android:layout_width="fill_parent"
    9 android:layout_height="wrap_content"
    10 android:lines="4"
    11 android:gravity="top"
    12 />
    13 <Button
    14 android:layout_width="wrap_content"
    15 android:layout_height="wrap_content"
    16 android:text="Save"
    17 android:onClick="writeFile"
    18 />
    19 <EditText
    20 android:layout_width="fill_parent"
    21 android:layout_height="wrap_content"
    22 android:lines="4"
    23 android:id="@+id/read_edit"
    24 android:gravity="top"
    25 android:editable="false"
    26 android:focusable="false"
    27 />
    28 <Button
    29 android:layout_width="wrap_content"
    30 android:layout_height="wrap_content"
    31 android:text="Read"
    32 android:onClick="readFile"
    33 />
    34 </LinearLayout>
     1 package com.turboradio.activity;
    2
    3 import java.io.FileInputStream;
    4 import java.io.FileNotFoundException;
    5 import java.io.FileOutputStream;
    6 import java.io.IOException;
    7
    8 import android.app.Activity;
    9 import android.os.Bundle;
    10 import android.view.View;
    11 import android.widget.EditText;
    12
    13 public class FileSaveActivity extends Activity{
    14 // 文件名称
    15 private static final String FILE_NAME = "temp.txt";
    16 private EditText writeEdit;
    17 private EditText readEdit;
    18 @Override
    19 protected void onCreate(Bundle savedInstanceState) {
    20 super.onCreate(savedInstanceState);
    21 setContentView(R.layout.file_save);
    22 writeEdit = (EditText)findViewById(R.id.write_edit);
    23 readEdit = (EditText)findViewById(R.id.read_edit);
    24 }
    25 /**
    26 * 写文件
    27 */
    28 public void writeFile(View v){
    29 write(writeEdit.getText().toString());
    30 }
    31 /**
    32 * 读文件
    33 */
    34 public void readFile(View v){
    35 readEdit.setText(read());
    36 }
    37 private String read(){
    38 try {
    39 FileInputStream fis = openFileInput(FILE_NAME);
    40 byte [] bytes = new byte [1024];
    41 fis.read(bytes);
    42 return new String(bytes);
    43 } catch (FileNotFoundException e) {
    44 e.printStackTrace();
    45 } catch (IOException e) {
    46 // TODO Auto-generated catch block
    47 e.printStackTrace();
    48 }
    49 return null;
    50 }
    51 private void write(String content){
    52 try {
    53 FileOutputStream fos = openFileOutput(FILE_NAME,MODE_APPEND);
    54 fos.write(content.getBytes());
    55 fos.close();
    56 } catch (FileNotFoundException e) {
    57 e.printStackTrace();
    58 } catch (IOException e) {
    59 e.printStackTrace();
    60 }
    61 }
    62 }




  • 相关阅读:
    iOS 自定义UITabBarController的tabBar
    iOS 设置导航栏之二(设置导航栏的颜色、文字的颜色、左边按钮的文字及颜色)
    iOS 设置导航栏的颜色和导航栏上文字的颜色
    iOS 修改UITextField的placeholder属性的字体颜色(修改UITextField占位符字体的颜色)
    iOS TPKeyboardAvoiding自动识别键盘的高度
    iOS 获取快递物流信息(GCD异步加载)
    iOS 图片循环滚动(切片效果)
    iOS block在两个页面间的简单传值
    swift
    iOS 10 之后权限设置
  • 原文地址:https://www.cnblogs.com/jiayonghua/p/2287357.html
Copyright © 2011-2022 走看看