Android 文件存储--内部存储的例子

Android 文件存储--内部存储的例子

1)新建Android 项目,项目名称:DemoInternalStorage
2) 在继承于Activity的类中编写相应代码,代码如下所示:
/*
* Copyright (C) Mesada Technologies Co., Ltd. 2005-2010.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Mesada Technologies Co., Ltd. ("Confidential Information").
* You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement
* you entered into with Mesada.
*/
package com.mesada.demo;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
* This is a demo about file storage.
*
* @author Xiaolong Long
* @date 2010-12-30
* @version 1.0
*/
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
private static final boolean mIsPrintInfo = true;

private static final String FILENAME = "temp.txt";

EditText mMsgView;
Button mSave;
Button mPrint;
Button mCancel;

boolean mIsLegal = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
if (mIsPrintInfo)
Log.i(TAG, "onCreate()...");

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupControlers();

mSave.setEnabled(false);
MsgTextWater msgTextWater = new MsgTextWater();
mMsgView.addTextChangedListener(msgTextWater);

mSave.setOnClickListener(this);
mPrint.setOnClickListener(this);
mCancel.setOnClickListener(this);
}

/**
*
* Find the views that were identified by the id attributes from the XML.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private void setupControlers() {
if (mIsPrintInfo)
Log.i(TAG, "setupControlers()...");

mMsgView = (EditText) findViewById(R.id.msg);
mSave = (Button) findViewById(R.id.saveMsg);
mPrint = (Button) findViewById(R.id.printMsg);
mCancel = (Button) findViewById(R.id.cancel);
}


/**
*
* Find the views that were identified by the id attributes from the XML.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private void setupControlers() {
if (mIsPrintInfo)
Log.i(TAG, "setupControlers()...");

mMsgView = (EditText) findViewById(R.id.msg);
mSave = (Button) findViewById(R.id.saveMsg);
mPrint = (Button) findViewById(R.id.printMsg);
mCancel = (Button) findViewById(R.id.cancel);
}

public void onClick(View v) {
if (mIsPrintInfo)
Log.i(TAG, "onClick()...");

// Returns this view's identifier.
int id = v.getId();
switch (id) {
case R.id.saveMsg:
try {
saveMsg();
Toast.makeText(MainActivity.this, R.string.success_write,
Toast.LENGTH_SHORT).show();
mMsgView.setText("");
} catch (IOException e) {
Log.e(TAG,
"failed to save the content to the file which called temp.txt",
e);
Toast.makeText(MainActivity.this, R.string.failed_write,
Toast.LENGTH_SHORT).show();
}
break;
case R.id.printMsg:
try {
mMsgView.requestFocus();
mMsgView.setText(getMsg());
} catch (IOException e) {
Log.e(TAG,
"failed to read a file from internal storage which called temp.txt",
e);
Toast.makeText(MainActivity.this, R.string.failed_read,
Toast.LENGTH_SHORT).show();
}
break;
case R.id.cancel:
finish();
break;
default:
break;
}
}
/**
*
* To create and write a file to the internal storage.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private void saveMsg() throws IOException {
String msg = String.valueOf(mMsgView.getText());
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
// To create and write a private file to the internal storage:
fos.write(msg.getBytes("utf-8"));
fos.flush();
fos.close();
}

/**
* To read a file from internal storage.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private String getMsg() throws IOException {
FileInputStream fis = openFileInput(FILENAME);
int length = FILENAME.length();
byte[] buffer = new byte[length];

ByteArrayOutputStream bos = new ByteArrayOutputStream();
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
fis.close();
bos.close();
return bos.toString();
}
class MsgTextWater implements TextWatcher {

public void afterTextChanged(Editable s) {

}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}

public void onTextChanged(CharSequence s, int start, int before,
int count) {
mIsLegal = validate(s);
if (mIsLegal) {
mSave.setEnabled(true);
return;
}
mSave.setEnabled(false);
}

/**
*
* To check the view if legal.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private boolean validate(CharSequence s) {
String ss = s.toString();
if (!"".equals(ss)) {
return true;
}
return false;
}
}
}
3)main.xml 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/enter_msg" />
<EditText
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right">
<Button
android:id="@+id/saveMsg"
android:text="@string/savemsg"
android:layout_width="180px"
android:layout_height="wrap_content"></Button>
<Button
android:id="@+id/printMsg"
android:text="@string/printmsg"
android:layout_width="145px"
android:layout_height="wrap_content"></Button>
<Button
android:id="@+id/cancel"
android:text="@string/cancel"
android:layout_width="145px"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>

4)AndroidMainfest.xml 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mesada.demo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="8" />

</manifest>
5)完成,顺便上传截图:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值